public ActionResult Upload(string dir = null) { //定义允许上传的文件扩展名 Dictionary <string, string> extTable = new Dictionary <string, string>(); extTable.Add("image", "gif,jpg,jpeg,png,bmp"); extTable.Add("flash", "swf,flv"); extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); String dirName = dir; if (String.IsNullOrEmpty(dirName)) { dirName = "image"; } if (!extTable.ContainsKey(dirName)) { return(showError("目录名不正确。")); } RequestUtil req = new RequestUtil(); var file = req.File("imgFile", false, "请选择文件。", extTable[dirName], "上传文件扩展名是不允许的扩展名。\n只允许" + extTable[dirName] + "格式。"); if (req.HasError) { return(showError(req.FirstError)); } String src = file.Save(); String fileUrl = RequestFile.FullUrl(src); return(Content(System.Web.Helpers.Json.Encode(new { error = 0, url = fileUrl, src = src }))); }
public ActionResult Upload(string dir = null) { //定义允许上传的文件扩展名 Dictionary<string, string> extTable = new Dictionary<string, string>(); extTable.Add("image", "gif,jpg,jpeg,png,bmp"); extTable.Add("flash", "swf,flv"); extTable.Add("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); extTable.Add("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); String dirName = dir; if (String.IsNullOrEmpty(dirName)) { dirName = "image"; } if (!extTable.ContainsKey(dirName)) { return showError("目录名不正确。"); } RequestUtil req = new RequestUtil(); var file = req.File("imgFile", false, "请选择文件。", extTable[dirName], "上传文件扩展名是不允许的扩展名。\n只允许" + extTable[dirName] + "格式。"); if (req.HasError) { return showError(req.FirstError); } String src = file.Save(); String fileUrl = RequestFile.FullUrl(src); return Content(System.Web.Helpers.Json.Encode(new { error = 0, url = fileUrl, src = src })); }