以下方法均是個人,僅供參考
public interface IFileHelper { /// <summary> /// 保存文件 (返回 Test.jpg) 出錯就返回 error|錯誤信息 /// </summary> string SaveFile(IFormFile file, FileCategory fc); bool DeleteFile(string fileName, FileCategory fc); }
public class FileHelper: IFileHelper { private readonly IHostingEnvironment _hostingEnv; private static readonly Random _rdm = new Random(); public FileHelper(IHostingEnvironment env) { _hostingEnv = env; } /// <summary> /// 保存文件(返回新文件名) /// </summary> /// <param name="file"></param> /// <param name="fc"></param> /// <returns></returns> public string SaveFile(IFormFile file, FileCategory fc) { var path = GetUploadPath(fc); var targetDirectory = Path.Combine(_hostingEnv.WebRootPath, string.Format(path)); //這裡進行隨機文件名 var fileName = GetRandName(file); var savePath = Path.Combine(targetDirectory, fileName); try { file.CopyTo(new FileStream(savePath, FileMode.Create)); //return Upload/NewsPhoto/Test.jpg //返回文件名 return fileName; } catch { return "false"; } } /// <summary> /// 刪除文件 /// </summary> /// <param name="fullPath"></param> /// <returns></returns> public bool DeleteFile(string fileName, FileCategory fc) { var path = GetUploadPath(fc); var targetDirectory = Path.Combine(_hostingEnv.WebRootPath, string.Format(path)); //物理完整路徑 var physicalPath = Path.Combine(targetDirectory, fileName); try { File.Delete(physicalPath); return true; } catch { return false; } } /// <summary> /// 相對路徑 /Upload/NewsPhoto/Test.jpg /// </summary> /// <param name="path"></param> /// <returns></returns> public static string GetFullPath(FileCategory fc,string fileName) { var path = GetUploadPath(fc); return Path.Combine(string.Format(path), fileName); } /// <summary> /// 獲取到上傳路徑(Upload//CasePhoto//) /// </summary> /// <param name="fc"></param> /// <returns></returns> public static string GetUploadPath(FileCategory fc) { switch (fc) { case FileCategory.CasePhoto:return "Upload//CasePhoto//"; case FileCategory.NewsPhoto:return "Upload//NewsPhoto//"; case FileCategory.CompanyPhoto: return "Upload//CompanyPhoto//"; case FileCategory.PositionPhoto: return "Upload//PositionPhoto//"; case FileCategory.Partner: return "Upload//Partner//"; default:return ""; } } public static string GetRandName(string oldFileName) { //獲取後綴 var extension= GetExtensionWithDot(oldFileName); //產生新的文件名 var newFileName = DateTime.Now.ToFileTime().ToString() + _rdm.Next(999); //組合 return newFileName + extension; } public static string GetRandName(IFormFile file) { var fileName = file.FileName; var randName = GetRandName(fileName); return randName; } public enum FileCategory { /// <summary> /// 案例文章插圖 /// </summary> CasePhoto, /// <summary> /// 新聞文章插圖 /// </summary> NewsPhoto, /// <summary> /// 公司介紹插圖 /// </summary> CompanyPhoto, /// <summary> /// 職位描述插圖 /// </summary> PositionPhoto, /// <summary> /// 合作伙伴 /// </summary> Partner, } /// <summary> /// 獲取到後綴名的方法 /// </summary> public static string GetExtensionWithDot(string fileName) { var dotIndex = fileName.LastIndexOf('.'); if (dotIndex < 0 || dotIndex >= fileName.Length) return string.Empty; return fileName.Substring(dotIndex); }
}
//添加一個FileHelper的依賴注入(依賴注入的實現類一定要寫構造方法)
//該方法是單例模式
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IFileHelper, FileHelper>(); }
在startup中加入
新建一個UploadController 專門處理上傳
public class UploadController : Controller { private readonly IFileHelper _fileHelper; public UploadController(IFileHelper fileHelper) { _fileHelper = fileHelper; } /// <summary> /// 專門為Case的文件上傳 有可能有兩個name 不支持H5 wangEditorFormFile 支持h5 wangEditorH5File /// </summary> /// <returns></returns> [HttpPost] public string UploadCompanyPhoto(IFormFile wangEditorFormFile = null, IFormFile wangEditorH5File = null) { //全部用/,windows支持/ return Upload(FileCategory.CompanyPhoto, wangEditorFormFile, wangEditorH5File); } private string Upload(FileCategory fc ,IFormFile wangEditorFormFile = null, IFormFile wangEditorH5File = null) { if (wangEditorFormFile == null && wangEditorH5File == null) { return "error|無文件上傳"; } var file = wangEditorFormFile == null ? wangEditorH5File : wangEditorFormFile; var result = _fileHelper.SaveFile(file, fc); //判斷是否錯誤 if (result == "false") { return "error|上傳失敗"; } else {
return "http://" + Request.Host.ToString() + "/" + FileHelper.GetFullPath(fc, result); ; } } }
以上方法僅供參考