8. upload files with mvc core, 8. mvccore
The following methods are for your reference only.
Public interface IFileHelper {// <summary> // if an error occurs when saving the file (Test.jpg is returned), an error is returned. | error message // </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> /// save the file (new file name is returned) /// </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); // random file name var fileName = GetRandName (file); var savePath = Path. combine (targetDirectory, fileName); try {file. copyTo (new FileStream (savePath, FileMode. create); // return Upload/NewsPhoto/Test.jpg // return the file name return fileName;} catch {return "false ";}} /// <summary> /// delete a file /// </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); // The physical full Path var physicalPath = path. combine (targetDirectory, fileName); try {File. delete (physicalPath); return true;} catch {return false ;}} /// <summary> /// relative path/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> /// obtain the Upload path (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) {// obtain the suffix var extension = GetExtensionWithDot (oldFileName); // generate a new file name 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> // case study illustration /// </summary> CasePhoto, /// <summary> /// news article illustration /// </summary> NewsPhoto, /// <summary> /// company illustration /// </summary> CompanyPhoto, /// <summary> /// job description illustration /// </summary> PositionPhoto, /// <summary> /// Partner /// </summary> Partner ,} /// <summary> /// method for obtaining the suffix /// </summary> public static string GetExtensionWithDot (string fileName) {var dotIndex = fileName. lastIndexOf ('. '); if (dotIndex <0 | dotIndex> = fileName. length) return string. empty; return fileName. substring (dotIndex );}
}
// Add a FileHelper dependency injection (The implementation class of dependency injection must write the constructor)
// This method is in singleton mode.
public void ConfigureServices(IServiceCollection services){ services.AddSingleton<IFileHelper, FileHelper>();}
Add
Create an UploadController dedicated to upload
Public class UploadController: Controller {private readonly IFileHelper _ fileHelper; public UploadController (IFileHelper fileHelper) {_ fileHelper = fileHelper ;} /// <summary> /// for Case files, two names may not support H5 wangEditorFormFile; h5 wangEditorH5File /// </summary> /// <returns> </returns> [HttpPost] public string UploadCompanyPhoto (IFormFile wangEditorFormFile = null, IFormFile wangEditorH5File = Null) {// All/, windows supports/return Upload (FileCategory. companyPhoto, wangEditorFormFile, callback);} private string Upload (FileCategory fc, IFormFile wangEditorFormFile = null, IFormFile encoding = null) {if (wangEditorFormFile = null & wangEditorH5File = null) {return "error | no file upload";} var file = wangEditorFormFile = null? WangEditorH5File: wangEditorFormFile; var result = _ fileHelper. saveFile (file, fc); // determines whether an error occurs. if (result = "false") {return "error | Upload Failed";} else {
Return "http: //" + Request. Host. ToString () + "/" + FileHelper. GetFullPath (fc, result );;}}}
The above method is for reference only.