MVC3 Valums Ajax文件上传

Sha*_*eKm 40 asp.net asp.net-mvc asp.net-mvc-3

我正在尝试使用valums ajax uploader.http://valums.com/ajax-upload/

我的页面上有以下内容:

var button = $('#fileUpload')[0];
var uploader = new qq.FileUploader({
    element: button,
    allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], 
    sizeLimit: 2147483647, // max size
    action: '/Admin/Home/Upload',
    multiple: false
});
Run Code Online (Sandbox Code Playgroud)

它会发布到我的控制器,但qqfile始终为null.我试过这些:

public ActionResult Upload(HttpPostedFile qqfile)
AND
HttpPostedFileBase file = Request.Files["file"];
Run Code Online (Sandbox Code Playgroud)

没有运气.

我在rails上找到了ruby的例子,但不确定如何在MVC中实现它 http://www.jigsawboys.com/2010/10/06/ruby-on-rails-ajax-file-upload-with-valum/

在firebug中我看到: http:// localhost:61143/Admin/Home/Upload?qqfile = 2glonglonglongname + - + Copy.gif

在此输入图像描述

在此输入图像描述 在此输入图像描述

在此输入图像描述

Sha*_*eKm 68

我想到了.这适用于IE和Mozilla.

    [HttpPost]
    public ActionResult FileUpload(string qqfile)
    {
        var path = @"C:\\Temp\\100\\";
        var file = string.Empty;

        try
        {
            var stream = Request.InputStream;
            if (String.IsNullOrEmpty(Request["qqfile"]))
            {
                // IE
                HttpPostedFileBase postedFile = Request.Files[0];
                stream = postedFile.InputStream;
                file = Path.Combine(path, System.IO.Path.GetFileName(Request.Files[0].FileName));
            }
            else
            {
                //Webkit, Mozilla
                file = Path.Combine(path, qqfile);
            }

            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            System.IO.File.WriteAllBytes(file, buffer);
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message }, "application/json");
        }

       return Json(new { success = true }, "text/html");
    }
Run Code Online (Sandbox Code Playgroud)

  • 看来(至少对我来说)IE不喜欢"application/json"内容类型,因为它要求下载结果.使用"text/html"似乎对两者都有效.仅供参考,如果其他人遇到同样的问题 (7认同)