下载uploadify文件
http://www.uploadify.com/
HTML(视图)
<html lang="zh-cn"> <head> <meta content="true"> <meta charset="UTF-8"> <script src="~/Areas/Test/Uploadify/jquery.uploadify.js"></script> <link href="~/Areas/Test/Uploadify/uploadify.css" rel="stylesheet" /> <title>uploadify多文件上传例子</title> </head> <body> <input type="file" id="file_upload" name="file_upload" /> </body> </html>
JS
<script> $(function () { $('#file_upload').uploadify({ 'swf': '@Url.Content("/Areas/Test/Uploadify/uploadify-v3.1/uploadify.swf")', 'uploader': '@Url.Action("UploadAppraisal")?resultPath=Upload', 'auto': true, 'fileTypeExts': '*.*', 'fileTypeDesc':'All Files', 'fileSizeLimit': '0', 'buttonText': '添加文件', 'width': 67, 'height': 19, 'margin-right': 0, multi: false, queueSizeLimit: 999, timeoutuploadLimit: 999, removeCompleted: true, uploadLimit: 999, 'onSelect': function (file) { }, 'onUploadStart' : function(file) { }, 'onUploadProgress': function (file, bytesUploaded, bytesTotal, totalBytesUploaded, totalBytesTotal) { }, 'onInit': function () { }, 'onUploadSuccess': function (file, data, response) { } }); });</script>
后台(MVC控制器)
public JsonResult UploadAppraisal(HttpPostedFileBase fileData, string resultPath) { if (fileData != null) { try { // 文件上传后的保存路径 string filePath = Server.MapPath("../Upload/temp"); string Size = HumanReadableFilesize(fileData.ContentLength); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string fileNames = Path.GetFileName(fileData.FileName);// 原始文件名称 string fileName = Regex.Match(fileNames, @".*?(?=[.])").Value;//去掉后缀名字的文件名字 string fileExtension = Path.GetExtension(fileNames); // 文件扩展名 string saveName = Guid.NewGuid().ToString() + fileExtension; // 保存文件名称 string path = filePath + "\" + saveName; string Filesize = HumanReadableFilesize(fileData.ContentLength);//字节大小 string FileDw = HumanReadableFile(fileData.ContentLength);//字节单位 fileData.SaveAs(path); return Json(new { Success = true, FileName = saveName}); } catch (Exception ex) { return Json(new { Success = false, Message = ex.Message }, JsonRequestBehavior.AllowGet); } } else { return Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet); } }
一个页面有多个上传控件
修改jquery.uploadify.js、jquery.uploadify.min.js
var mydate = new Date();
this.movieName = "SWFUpload_" + mydate.getTime().toString();
上传大文件(Web.config)
Web.config添加一下代码
<system.web> <compilation debug="true" targetFramework="4.5" /> <!--maxRequestLength就是文件的最大字符数,最大值不能超过2个G左右,executionTimeout是超时时间--> <httpRuntime targetFramework="4.5" maxRequestLength="1073741824" executionTimeout="3600" /> </system.web> <system.webServer> <security> <requestFiltering> <!--修改服务器允许最大长度--> <requestLimits maxAllowedContentLength="1073741824"/> </requestFiltering> </security> </system.webServer>