本来想做一套上传公用的组建的,里面包含文件转码等功能,看来这些都只能后来一步一步加上了,先写下来。。。
1,引入脚本等
@{ Layout = null; } <!DOCTYPE html> <html> <head> <title>Index</title> <link href="/uploadify/uploadify.css" rel="stylesheet" /> </head> <body> <input type="file" name="upload" id="upload" /> </body> </html> <script src="/Scripts/jquery-1.11.1.min.js"></script> <script src="/uploadify/jquery.uploadify.min.js"></script> <script> $(function () { $(‘#upload‘).uploadify({ ‘formData‘: { ‘folder‘: ‘d:\\‘ }, ‘buttonText‘: ‘选择文件‘, ‘buttonClass‘: ‘browser‘, ‘removeCompleted‘: false, ‘swf‘: ‘/uploadify/uploadify.swf‘, ‘uploader‘: ‘/FileUp/Upload‘, ‘fileSizeLimit‘:‘500MB‘, ‘onError‘: function (event, id, fileObj, errorObj) { if (errorObj.type === "File Size") { alert(‘超过文件上传大小限制(2M)!‘); return; } alert(errorObj.type + ‘, Error: ‘ + errorObj.info); }, }); }); </script>
2,后台上传代码
public class FileUpController : Controller { public ActionResult Index() { return View(); } public ContentResult Upload(HttpPostedFileBase fileData, string folder) { string filename = ""; if (null != fileData) { var length = fileData.ContentLength; try { filename = Path.GetFileName(fileData.FileName); //获得文件名 saveFile(fileData, folder, filename); } catch (Exception ex) { filename = ex.ToString(); } } return Content(filename); } [NonAction] private bool saveFile(HttpPostedFileBase postedFile, string filepath, string saveName) { bool result = false; if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } try { postedFile.SaveAs(Path.Combine(filepath, saveName)); result = true; } catch (Exception e) { throw new ApplicationException(e.Message); } return result; } }
3,设置web.config(在system.web节点中)
<httpRuntime requestLengthDiskThreshold="256" maxRequestLength="2097151"/> //假如超过256kb文件,其将缓存到硬盘
时间: 2024-10-13 15:15:04