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://www.cnblogs.com/igqx/p/"https://localhost:44370/api/FileOperation/DownLoad?Url=/FileUpload/132211303318715030.xls" > 下載 </ a >
<input type = "file" id="f1" />
<input type = "button" value=https://www.cnblogs.com/igqx/p/"aa" onclick="ff()"/> <script> function ff() { var formData = https://www.cnblogs.com/igqx/p/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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/3514.html
標籤:C#
