1.首先使用VS創建WebAPI項目 (這裡有個幫助類,將此幫助類複製到項目里,有興趣可以學著寫) //文件上傳下載,導入導出輔助類 public class APIFileHelp { //此為限制文件格式 public string[] ExtentsfileName = new string[ ...
1.首先使用VS創建WebAPI項目
(這裡有個幫助類,將此幫助類複製到項目里,有興趣可以學著寫)
//文件上傳下載,導入導出輔助類 public class APIFileHelp { //此為限制文件格式 public string[] ExtentsfileName = new string[] { ".doc", ".xls", ".png", ".jpg" }; //Upload為自定義的文件夾,在項目中創建 public string UrlPath = "/Upload/"; //響應對象 ,使用前先賦值 public HttpResponse Response = HttpContext.Current.Response; public HttpRequest Request = HttpContext.Current.Request; //文件下載 //downFileName下載後保存名,sourceFileName伺服器端物理路徑 public void DownLoad(string downFileName, string sourceFileName) { if (File.Exists(sourceFileName)) { Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", downFileName)); Response.Charset = "GB2312"; Response.ContentEncoding = Encoding.GetEncoding("GB2312"); Response.ContentType = MimeMapping.GetMimeMapping(downFileName); Response.WriteFile(sourceFileName); Response.Flush(); Response.Close(); } } //文件上傳操作 public FileResult UpLoad() { //保存文件名 string name = ""; if (Request.Files.Count > 0) { string FileUrlResult = ""; foreach (string fn in Request.Files) { var file = Request.Files[fn]; name = file.FileName.ToString(); var extenfilename = Path.GetExtension(file.FileName); //判斷 路徑是否存在 string path = HttpContext.Current.Server.MapPath(UrlPath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } if (ExtentsfileName.Contains(extenfilename.ToLower())) { string urlfile = UrlPath + DateTime.Now.ToFileTime() + extenfilename; string filepath = HttpContext.Current.Server.MapPath(urlfile); file.SaveAs(filepath); FileUrlResult += urlfile + ";"; } //格式不正確 else {
//實例化結果類 return new FileResult() { Code = -1, Msg = "只允許上傳指定格式文件" + string.Join(",", ExtentsfileName), Url = "" }; } } //上傳成功 return new FileResult() { Code = 0, Name = name, Msg = "上傳成功", Url = FileUrlResult }; } //上傳失敗 else { return new FileResult() { Code = -1, Msg = "不能上傳空文件", Url = "" }; } } } //結果類 public class FileResult { public int Code { get; set; } //結果 public string Msg { get; set; } //提示信息 public string Url { get; set; } //地址 public string Name { get; set; } //文件名 }
//控制器端 public class FileOperationController : ApiController {
//實例化幫助類 APIFileHelp help = new APIFileHelp(); [HttpGet] public void DownLoad(string Url) { string filePath = HttpContext.Current.Server.MapPath(Url); FileInfo fi = new FileInfo(filePath); help.DownLoad(fi.Name, fi.FullName); } //文件上傳 [HttpPost] public FileResult UpLoad() { return help.UpLoad(); } }
//文件上傳、下載: MVC端
//文件下載 //API地址+參數+自己定義的文件名+文件Url <a href = "https://localhost:44370/api/FileOperation/DownLoad?Url=/FileUpload/132211303318715030.xls" > 下載 </ a >
<input type = "file" id="f1" />
<input type = "button" value="aa" onclick="ff()"/> <script> function ff() { var formData = new FormData(); var file = document.getElementById("f1").files[0]; formData.append("fileInfo", file); $.ajax({ url: "https://localhost:44370/api/FileOperation/UpLoad", type: "POST", data: formData, contentType: false,//必須false才會自動加上正確的Content-Type processData: false,//必須false才會避開jQuery對 formdata 的預設處理,XMLHttpRequest會對 formdata 進行正確的處理 success: function(data) { if (data.Code == 0) alert(data.Msg) else alert(data.Url) }, error: function(data) { alert("上傳失敗!"); } }); } </script>