我使用Valums阿贾克斯上传。所有在Mozilla与此code的伟大工程:
查看:
VAR键= $('#文件上传')[0];VAR上传=新qq.FileUploader({ 元素:按钮, allowedExtensions:JPG,JPEG,PNG,GIF] 的sizeLimit:2147483647,//最大尺寸 动作:'/管理/主页/上传', 多:假的});控制器:
公众的ActionResult上传(字符串qqfile){ VAR流= Request.InputStream; VAR缓冲=新的字节[stream.Length] stream.Read(缓冲液,0,buffer.Length); VAR路径=使用Server.Mappath(〜/ App_Data文件); var文件= Path.Combine(路径,qqfile); File.WriteAllBytes(文件,缓冲液); // TODO:返回无论上传控件预计为响应}这是回答了在这个职位:
MVC3 Valums阿贾克斯文件上传
然而问题是,这并不工作在IE。我确实发现这一点,但我无法弄清楚如何实现它:
IE不发送数据流中 request.InputStream...而不是弄 通过输入流 HttpPostedFileBase从 Request.Files []集合
此外,这在这里显示这个家伙是怎么做到的,但我不知道如何为我的项目更改:
Valum文件上传 - 适用于Chrome,但不是IE,图片IMG = Image.FromStream(Request.InputStream)
//这适用于IE浏览器HttpPostedFileBase httpPostedFileBase = Request.Files [0]
作为HttpPostedFileBase;
想不通这一个。请帮忙!谢谢
解决方案我想通了。这在IE和Mozilla。
[HttpPost] 公众的ActionResult文件上传(字符串qqfile) { VAR路径= @C:\\\\ \\\\温度100 \\\\; var文件=的String.Empty; 尝试 { VAR流= Request.InputStream; 如果(String.IsNullOrEmpty(请求[qqfile])) { // IE HttpPostedFileBase postedFile = Request.Files [0]; 流= postedFile.InputStream; 文件= Path.Combine(路径,System.IO.Path.GetFileName(Request.Files [0] .FileName)); } 其他 { // Webkit内核,Mozilla的 文件= Path.Combine(路径,qqfile); } VAR缓冲=新的字节[stream.Length] stream.Read(缓冲液,0,buffer.Length); System.IO.File.WriteAllBytes(文件,缓冲液); } 赶上(异常前) { 返回JSON(新{成功= FALSE,消息= ex.Message},应用/ JSON); } 返回JSON(新{成功= TRUE},text / html的); }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
解决方案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"); }
更多推荐
MVC Valums阿贾克斯上传
发布评论