方法1、FormData简单实现
后端:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Mao.Common { public enum UploadFileType { Video, Image } public class FileUploadHelper { #region Fields private bool m_statu; private string m_msg; private string m_filename; #endregion #region 构造方法 /// <summary> /// 初始化 /// </summary> public FileUploadHelper() { m_statu = false; m_msg = ""; m_filename = ""; } #endregion #region 公开属性 /// <summary> /// 处理结果 /// </summary> public bool Statu { get { return m_statu; } } public string Msg { get { return m_msg; } } public string FileName { get { return m_filename; } } #endregion /// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="path">Server.MapPath转换后的路径</param> /// <returns></returns> public void SaveFile(HttpPostedFileBase file, string path) { if (file != null && file.ContentLength > 0) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string ext1 = Path.GetExtension(file.FileName); //格式判断 if (ext1 != ".gif" && ext1 != ".jpg" && ext1 != ".jpeg" && ext1 != ".png" && ext1 != ".mp4") { m_statu = false; m_msg = "文件格式不正确!"; } else { string name = DateTime.Now.ToString("yyyyMMddHHmmssff"); string ext = Path.GetExtension(file.FileName); string downpath = path + name + ext; string filepath = path + name + ext; file.SaveAs(filepath); m_statu = true; m_msg = "上传成功!"; m_filename = name + ext; } } else { m_statu = false; m_msg = "未接收到文件!"; } } /// <summary> /// /// </summary> /// <param name="file"></param> /// <param name="type">限定文件类型</param> /// <param name="path"></param> public void SaveFile(HttpPostedFileBase file, UploadFileType type, string path) { string ext1 = Path.GetExtension(file.FileName); switch (type) { case UploadFileType.Image: if (ext1 != ".gif" && ext1 != ".jpg" && ext1 != ".jpeg" && ext1 != ".png") { m_statu = false; m_msg = "上传失败!文件格式不正确!"; } else { m_statu = true; m_msg = "上传成功!"; } break; case UploadFileType.Video: if (ext1 != ".mp4") { m_statu = false; m_msg = "上传失败!文件格式不正确!"; } else { m_statu = true; m_msg = "上传成功!"; } break; default: m_statu = false; m_msg = "上传失败!未知文件格式!"; break; } if (m_statu) { this.SaveFile(file, path); } } } }
public ActionResult Upload(HttpPostedFileBase file, string type) { string path = null; UploadFileType fileType; if (string.Equals(type.ToLower(), "video", StringComparison.OrdinalIgnoreCase)) { fileType = UploadFileType.Video; path = ConfigurationManager.AppSettings.Get("VideoUploadPath"); } else { fileType = UploadFileType.Image; path = ConfigurationManager.AppSettings.Get("ImagesUploadPath"); } FileUploadHelper filehelper = new FileUploadHelper(); filehelper.SaveFile(file, fileType, Server.MapPath(path)); return Json(new { statu = filehelper.Statu, msg = filehelper.Msg, filename = filehelper.FileName }); }
前端:
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data", id = "uploadForm" })) { <input type="file" style="height: 34px;" name="file" accept="image/png, image/jpeg, image/gif, image/jpg" /> <input type="hidden" name="type" value="image" /> } …… function doUpload() { var formData = new FormData($("#uploadForm")[0]); if (!$("input[type=file]").val()) {return;} $.ajax({ url: '/upload/upload', type: 'POST', data: formData, async: false, cache: false, contentType: false, processData: false, success: function (returndata) { if (!returndata.statu) { alert(returndata.msg); } else { $("#ImagePath").val(returndata.filename); } }, error: function (returndata) { alert(returndata); } }); }
方法2、