Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Valums Ajax Uploader - IE doesn't send the stream in request.InputStream

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

like image 207
ShaneKm Avatar asked Feb 03 '11 16:02

ShaneKm


1 Answers

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");
        }
like image 83
ShaneKm Avatar answered Nov 09 '22 02:11

ShaneKm