1

我最近尝试使用http://malsup.com/jquery/form/#file-upload进行文件上传,但我不完全确定如何将图像上传到服务器上的某个文件夹。

继承人的jQuery:

(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
        //console.log(percentVal, position, total);
    },
    complete: function(xhr) {
        status.html(xhr.responseText);
    }
}); 

})();

然后是 HTML:

<h1>File Upload Progress Demo #3</h1>
<code>&lt;input type="file" name="myfile[]"></code><br>
<code>&lt;input type="file" name="myfile[]"></code><br>
<code>&lt;input type="file" name="myfile[]"></code>
<form action="files-raw.php" method="post" enctype="multipart/form-data">
    <input type="file" name="myfile[]"><br>
    <input type="file" name="myfile[]"><br>
    <input type="file" name="myfile[]"><br>
    <input type="submit" value="Upload File to Server">
</form>

<div class="progress">
    <div class="bar"></div >
    <div class="percent">0%</div >
</div>

<div id="status"></div>
4

2 回答 2

0

服务器端代码负责将上传的文件保存到服务器,当您使用 PHP 时,您可以通过$_FILES["myfile"]. 我假设你想要这样的代码,

$newdirectory = "/your/directory";
$count = 0;
foreach ($_FILES['myfile']['name'] as $filename)
{
    $temp = $_FILES['myfile']['tmp_name'][$count];
    move_uploaded_file($temp, $newdirectory . '/' . basename($filename));
    $count++;
}

这应该按照您的要求进行。有关在此处此处处理上传的更多信息move_uploaded_file

于 2013-01-29T09:11:33.010 回答
0

如果它在 MVC 中,那么在视图中

<form action="Home/HandleFileUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="myfile[]"><br>
    <input type="file" name="myfile[]"><br>
    <input type="file" name="myfile[]"><br>
    <input type="submit" value="Upload File to Server">
</form>

并在控制器中写下这个......

[HttpPost]
        public ActionResult HandleFileUpload()
        {
            if (!string.IsNullOrEmpty(Request.Headers["X-File-Name"]))
            {
                string path = Server.MapPath(string.Format("~/Uploads/{0}", Request.Headers["X-File-Name"]));
                using (var fileStream = new FileStream(path, FileMode.OpenOrCreate))
                {
                    Request.InputStream.CopyTo(fileStream);
                }

                return this.Json(new { success = true });
            }

            return this.Json(new { success = false });
        }
       }
于 2013-01-29T08:56:22.480 回答