上传图片
public class UserFile { /// <summary> /// 文件名称 /// </summary> public string FileName { get; set; } /// <summary> /// 文件大小 /// </summary> public long Length { get; set; } /// <summary> /// 文件类型 /// </summary> public string Extension { get; set; } /// <summary> /// 文件类型 /// </summary> public string FileType { get; set; } /// <summary> /// 图片归类 1、前台 2、后台 /// </summary> public string MessageType { get; set; } private readonly static string[] Filters = { ".jpg", ".png", ".bmp",".jpeg",".JPG" ,".PNG",".BMP",".JPEG"}; public bool IsValid => !string.IsNullOrEmpty(this.Extension) && Filters.Contains(this.Extension); private IFormFile file; public IFormFile File { get { return file; } set { if (value != null) { this.file = value; this.FileType = this.file.ContentType; this.Length = this.file.Length; this.Extension = this.file.FileName.Substring(file.FileName.LastIndexOf('.')); if (string.IsNullOrEmpty(this.FileName)) this.FileName = this.FileName; } } } public async Task<string> SaveAs(string destinationDir = null) { if (this.file == null) throw new ArgumentNullException("没有需要保存的文件"); if (destinationDir != null) Directory.CreateDirectory(destinationDir); var newName = DateTime.Now.Ticks; var newFile = Path.Combine(destinationDir ?? "", $"{newName}{this.Extension}"); using (FileStream fs = new FileStream(newFile, FileMode.CreateNew)) { await this.file.CopyToAsync(fs); fs.Flush(); } int b = newFile.LastIndexOf(@"\")+1; return newFile.Substring(b,newFile.Length-b); } }
public class FromFileAttribute : Attribute, IBindingSourceMetadata { public BindingSource BindingSource => BindingSource.FormFile; } public class UploadImage : AppServiceBase { private readonly IHostingEnvironment _hostingEnvironment; public UploadImage(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } #region 单张图片上传 /// <summary> /// 单张图片上传 /// </summary> /// <param name="file"></param> /// <returns></returns> [AbpAllowAnonymous] [HttpPost] public async Task<IActionResult> Post([FromFile]UserFile file) { string webRootPath = _hostingEnvironment.WebRootPath; string newFile = string.Empty; var fileName = DateTime.Now.ToString("yyyyMMdd"); if (file == null || !file.IsValid) return new JsonResult(new { code = 500, message = "不允许上传的文件类型" }); if (file != null) { switch (file.MessageType)//上传的文件夹类型 { case "1"://前台 newFile = await file.SaveAs(webRootPath + "/Wap/"+ fileName+"/"); break; case "2"://后台 newFile = await file.SaveAs(webRootPath + "/Admin/"+ fileName +"/"); break; default: break; } } return new JsonResult(new { code = 0, message = "成功", url = newFile.Replace("wwwroot", "") }); } #endregion [AbpAllowAnonymous] [HttpPost] public async Task<IActionResult> UploadFile([FromFile]UserFile file) { string webRootPath = _hostingEnvironment.WebRootPath; string newFile = string.Empty; var fileName = DateTime.Now.ToString("yyyyMMdd"); if (file == null || !file.IsValid) return new JsonResult(new { code = 500, message = "不允许上传的文件类型" }); if (file != null) { switch (file.MessageType)//上传的文件夹类型 { case "1"://前台 newFile = await file.SaveAs(webRootPath + "/Wap/" + fileName + "/"); break; case "2"://后台 newFile = await file.SaveAs(webRootPath + "/Admin/" + fileName + "/"); break; default: break; } } return new JsonResult(new { code = 0, message = "成功", url = newFile.Replace("wwwroot", "") }); } #region 删除图片 /// <summary> /// 删除图片 /// </summary> /// <param name="file">图片名.扩展名 </param> /// <returns></returns> [AbpAllowAnonymous] [HttpPost] public async Task<IActionResult> DeleteFile(string file,string MessageType) { string webRootPath = _hostingEnvironment.WebRootPath; string fileurl = ""; var fileName = DateTime.Now.ToString("yyyyMMdd"); if (file != null) { if (File.Exists(fileurl)) { switch (MessageType)//上传的文件夹类型 { case "1"://前台 fileurl = webRootPath + "/Wap/"+ fileName+"/" + file; File.Delete(fileurl); break; case "2"://后台 fileurl = webRootPath + "/Admin/" + fileName + "/" + file; File.Delete(fileurl); break; default: break; } } } return new JsonResult(new { code = 0, message = "成功", url = fileurl }); } #endregion #region 多张图片上传 /// <summary> /// 多张图片上传 /// </summary> /// <param name="files"></param> /// <returns></returns> [AbpAllowAnonymous] [HttpPost] public async Task<List<string>> UploadPhotosAsync([FromForm]IFormFileCollection files,string MessageType) { List<string> fileurl = new List<string>(); string webRootPath = _hostingEnvironment.WebRootPath; long size = files.Sum(f => f.Length); var Parfile = DateTime.Now.ToString("yyyyMMdd"); var fileFolder = string.Empty; switch (MessageType)//上传的文件夹类型 { case "1"://前台 fileFolder = Path.Combine(webRootPath + "/Wap/" + Parfile + "/"); break; case "2"://后台 fileFolder = Path.Combine(webRootPath + "/Admin/" + Parfile + "/"); break; default: break; } if (!Directory.Exists(fileFolder)) Directory.CreateDirectory(fileFolder); foreach (var file in files) { if (file.Length > 0) { var fileName = DateTime.Now.Ticks + Path.GetExtension(file.FileName); //var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + // Path.GetExtension(file.FileName); var filePath = Path.Combine(fileFolder, fileName); using (var stream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(stream); } int b = filePath.LastIndexOf(@"\") + 1; string path = filePath.Substring(b, filePath.Length - b); fileurl.Add(path.Replace("wwwroot", "")); } } return fileurl; } #endregion }