0

在没有提交的情况下从文件对话框中选择文件后,如何自动调用我的控制器方法,以便我可以将该文件保存在临时文件夹中,直到用户准备好将文件保存并存储到文件系统。这种方法理想吗?我的目标是最终让用户上传一个短视频,将其保存到临时文件夹,让他/她在视图中看到一个缩略图,并且他/她希望他们可以将其保存到那里的配置文件中。

<form id="fileupload" action="/Home/UploadFiles" method="POST" 
               enctype="multipart/form-data">
  <input id="fileupload" type="file" onchange="uploadSelectedFile(this)" 
               name="file">
  @*<input type="submit" />*@
</form>

在控制器中:

[HttpPost]
public ActionResult UploadFiles(HttpPostedFileBase file)
{
     //Save file to temp folder 
     //then later user can call a save button to acutally save the files.
     return Json();
}
4

2 回答 2

1

更新:显然,uploadify 使用密钥“Filedata”发送它的文件。我在这里写了一篇带有完整 asp.net-mvc/uploadify 示例的文章。


我认为您不能依赖常规文件上传。

用户上传文件后,您必须在后面进行 ajax 上传。有关您可以使用的一些现成控件,请参阅此链接。

Uploadify看起来很有前途,是一个JQuery 插件。这就是使用uploadify 的方式:

<input type="file" name="file_upload" id="file_upload" />

$(function() {
    $("#file_upload").uploadify({
        'swf'      : '/uploadify/uploadify.swf',
        'uploader' : '<path_to_your_action_method>' //Youcontroller/UploadFiles
    });
});

在您的控制器中:

[HttpPost]
public ActionResult UploadFiles()
{
     //Uploadify sends this as "Filedata"
     HttpPostedFile theFile = Request.Files["Filedata"];
     //save your file here...

     //anything you return will be sent to the onUploadSucess event
     http://www.uploadify.com/documentation/uploadify/onuploadsuccess/
     return Json();
}

当用户然后选择保存时,您可以确定他是否保存了与用户有某种关联的文件并将文件传输到正确的位置。

于 2012-12-13T06:57:16.573 回答
1

如前所述gideon,我认为您无法通过正常的文件上传来做到这一点。

您可以使用Uploadify文件上传控制来做到这一点。

 $('#file_upload').uploadify({
                'checkExisting': 'Content/uploadify/check-exists.php',
                'swf': '/Content/uploadify/uploadify.swf',
                'uploader': '/Home/uploadify',
                'auto': false,
                'buttonText': 'Browse'
});

控制器中的代码是

[HttpPost]
        public ActionResult Uploadify(IEnumerable<HttpPostedFileBase> fileData)
        {
            foreach (var file in fileData)
            {
                if (file.ContentLength > 0)
                {

                    currpath = Path.Combine(System.Environment.GetEnvironmentVariable("TEMP"), file.FileName);

                    file.SaveAs(currpath);
                }
            }
            return View();
        }

如果您要上传单个文件,请使用HttpPostedFileBase而不是IEnumerable<HttpPostedFileBase>

希望这可以帮助。

于 2012-12-13T07:10:43.447 回答