Sha*_*eKm 10 asp.net asp.net-mvc file-upload
我正在使用Valums Ajax上传器.所有在Mozilla中运行良好的代码:
视图:
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
});
Run Code Online (Sandbox Code Playgroud)
控制器:
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
}
Run Code Online (Sandbox Code Playgroud)
在这篇文章中回答:
但问题是,这在IE中不起作用.我找到了这个,但我无法弄清楚如何实现它:
IE不会在"request.InputStream"中发送流...而是从Request.Files []集合中通过HttpPostedFileBase获取输入流
此外,这里显示了这个人是如何做到的,但我不知道如何改变我的项目:
Valum文件上传 - 适用于Chrome但不适用于IE,Image img = Image.FromStream(Request.InputStream)
Run Code Online (Sandbox Code Playgroud)//This works with IE HttpPostedFileBase httpPostedFileBase = Request.Files[0]作为HttpPostedFileBase;
无法想出这个.请帮忙!谢谢
Sha*_*eKm 16
我想到了.这适用于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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6972 次 |
| 最近记录: |