MVC3 Valums Ajax文件上传

时间:2011-02-03 10:23:33

标签: 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
});

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

public ActionResult Upload(HttpPostedFile qqfile)
AND
HttpPostedFileBase file = Request.Files["file"];
没有任何运气。

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

在萤火虫中我看到了这个: http://localhost:61143/Admin/Home/Upload?qqfile=2glonglonglongname+-+Copy.gif

enter image description here

enter image description here enter image description here

enter image description here

5 个答案:

答案 0 :(得分: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");
    }

答案 1 :(得分:1)

此组件正在发送application/octet-stream而不是multipart/form-data,这是默认模型绑定器可以使用的。因此,您不能指望Request.Files具有此类请求的任何值。

您需要手动读取请求流:

public ActionResult Upload(string qqfile)
{
    var stream = Request.InputStream;
    var buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);

    var path = Server.MapPath("~/App_Data");
    var file = Path.Combine(path, qqfile);
    File.WriteAllBytes(file, buffer);

    // TODO: Return whatever the upload control expects as response
}

答案 2 :(得分:1)

使用multipart-mime上传IE。其他浏览器使用Octet-Stream。

我写了一个上传处理程序来处理Valums Ajax Uploader,它可以同时使用MVC和Webforms&两种上传方式。如果你愿意,我很乐意与你分享。它紧跟PHP处理程序。

我处理上传的控制器如下所示:

public class UploadController : Controller
{
    private IUploadService _Service;

    public UploadController()
        : this(null)
    {
    }

    public UploadController(IUploadService service)
    {
        _Service = service ?? new UploadService();
    }

    public ActionResult File()
    {
        return Content(_Service.Upload().ToString());
    }

UploadService看起来像这样:

public class UploadService : IUploadService
{
    private readonly qq.FileUploader _Uploader;

    public UploadService()
        : this(null)
    { }

    public UploadService(IAccountService accountservice)
    {
        _Uploader = new qq.FileUploader();
    }

    public UploadResult Upload()
    {
        qq.UploadResult result = _Uploader.HandleUpload();
        if (!result.Success)
            return new UploadResult(result.Error);

                     .... code .....

        return new UploadResult((Guid)cmd.Parameters["@id"].Value);
        }
        catch (Exception ex)
        {
            return new UploadResult(System.Web.HttpUtility.HtmlEncode(ex.Message));
        }
        finally
        {
                      ............code.........
        }

    }

   ...............code ............

答案 3 :(得分:1)

你应该尝试:

Stream inputStream = (context.Request.Files.Count > 0) ? context.Request.Files[0].InputStream : context.Request.InputStream;

答案 4 :(得分:0)

我在ASP.Net 4.0中开发,但我们没有MVC架构。几天前我有同样的问题。但是,我想出来了,这是我的解决方案。

//For IE Browser
HttpPostedFile selectedfile = Request.Files[0];
System.Drawing.Bitmap obj = new System.Drawing.Bitmap(selectedfile.InputStream);

//For Non IE Browser
System.Drawing.Bitmap obj = new System.Drawing.Bitmap(Request.InputStream);

现在,您可以使用obj进行进一步操作。