MVC Valums Ajax Uploader - IE doesn't send the

2019-02-01 14:21发布

问题:

i'm using Valums Ajax uploader. all works great in Mozilla with this code:

View:

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
});

Controller:

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
}

which was answered in this post:

MVC3 Valums Ajax File Upload

However issue is that this doesn't work in IE. I did find this but i can't figure out how to implement it:

IE doesn't send the stream in "request.InputStream" ... instead get the input stream through the HttpPostedFileBase from the Request.Files[] collection

Also, this here that shows how this guy did it but i'm not sure how to change for my project:

Valum file upload - Works in Chrome but not IE, Image img = Image.FromStream(Request.InputStream)

//This works with IE
HttpPostedFileBase httpPostedFileBase = Request.Files[0]

as HttpPostedFileBase;

can't figure this one out. please help! thanks

回答1:

I figured it out. This works in IE and 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");
        }


回答2:

Shane's solution works but it seems like the Request["qqfile"] is being set anyway in IE. Not sure if this is because I'm using an updated version of the fileuploader but I've modified the "if" statement to make it work for IE (checking if there are any uploaded files in the request).

if (Request.Files.Count > 0) {
    //ie
} else {
    //webkit and mozilla
}

Here is the full snippet

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

    try
    {
        var stream = Request.InputStream;
        if (Request.Files.Count > 0)
        {
            // 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");
}