Asp.net mvc使用 Ajax Upload上传文件
Ajax Upload
主页 http://valums.com/ajax-upload/
Ajax Upload文件上传插件允许你上传多个插件而无需刷新页面,可以使用任何的元素来显示文件选择窗口。它可以在所有主流的浏览器下工作,从2.0版本开始,不需要任何库运行。Ajax Upload文件上传插件不会污染任何命名空间,所以它与jQuery,Prototypejs,mootools其他JavaScript库兼容。
新版基于
XHR 上传文件,对于不支持XHR的浏览器为普通文件上传
如何使用?
引用如下文件
<link href="../Scripts/ajax-upload/fileuploader.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/ajax-upload/fileuploader.js" type="text/javascript"></script>
js:
1 function createUploader() {
2 var uploader = new qq.FileUploader({
3 element: document.getElementById('ID'),
4 action: '/Service/UpLoad',
5 onSubmit: function (id, fileName) {},
6 onComplete: function (id, fileName, responseJSON) {}
7 });
8 }
9
2 var uploader = new qq.FileUploader({
3 element: document.getElementById('ID'),
4 action: '/Service/UpLoad',
5 onSubmit: function (id, fileName) {},
6 onComplete: function (id, fileName, responseJSON) {}
7 });
8 }
9
10 window.onload = createUploader;
其他属性 参考(旧版)
注意:多个属性在新版中已失效
服务端:
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");
}
{
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");
}