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:
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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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");
}
Method 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");
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0