public FileService( IWebHostEnvironment env, ICoreUnitOfWork uowCore, IOptions <FileUploadOption> options) { _env = env; _uowCore = uowCore; _options = options.Value; }
public static bool CheckSupportType(this Stream stream, FileUploadOption _option) { if (_option.SupportType == null || !_option.SupportType.Any()) { return(true); } var fileClass = GetFileClass(stream); if (_option.SupportType.Any(x => x.Value.Equals(fileClass))) { return(true); } else { return(false); } }
/// <summary> /// 通过后缀检测文件是否支持上传 /// </summary> /// <param name="fileName"></param> /// <param name="_option"></param> /// <returns></returns> public static bool CheckExtesionSupportType(this string fileName, FileUploadOption _option) { if (_option.SupportType == null || !_option.SupportType.Any()) { return(true); } if (string.IsNullOrEmpty(fileName)) { return(false); } var extesion = Path.GetExtension(fileName); if (string.IsNullOrEmpty(extesion)) { return(true); } else { extesion = extesion.Substring(1); return(_option.SupportType.Any(x => x.Key.Contains(extesion))); } }
/// <summary> /// 如果文件上传成功,那么message会返回一个上传文件的路径,如果失败,message代表失败的消息 /// </summary> /// <param name="request"></param> /// <param name="targetDirectory"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public static async Task <(bool success, IResultModel result, FormValueProvider valueProvider)> StreamFile(this HttpRequest request, FileUploadOption option, CancellationToken cancellationToken) { //读取boundary var boundary = request.GetMultipartBoundary(); if (string.IsNullOrEmpty(boundary)) { return(false, ResultModel.Failed("解析失败"), null); } //检查相应目录 var childDir = Path.Combine(option.Module, DateTime.Now.ToString("yyyyMM")); var fileDirectory = Path.Combine(option.Root, childDir); if (!Directory.Exists(fileDirectory)) { Directory.CreateDirectory(fileDirectory); } //准备文件保存路径 var fileReqestPath = string.Empty; var physicalFilefilePath = string.Empty; //准备viewmodel缓冲 var accumulator = new KeyValueAccumulator(); //创建section reader var reader = new MultipartReader(boundary, request.Body); FileInfoParam fileInfo = new FileInfoParam(); try { var section = await reader.ReadNextSectionAsync(cancellationToken); while (section != null) { ContentDispositionHeaderValue header = section.GetContentDispositionHeader(); if (header.FileName.HasValue || header.FileNameStar.HasValue) { var fileSection = section.AsFileSection(); //检测上传类型 var originalfileName = header.FileName.HasValue ? header.FileName.Value : ""; if (!originalfileName.CheckExtesionSupportType(option) || !fileSection.FileStream.CheckSupportType(option)) { var errorExtension = ""; if (header.FileName.HasValue) { errorExtension = Path.GetExtension(header.FileName.Value); } return(false, ResultModel.Failed($"文件类型不支持上传:{errorExtension}"), null); } fileInfo = GetFileInfo(fileSection); //var fileName = fileSection.FileName; var fileName = $"{Guid.NewGuid().ToString("N")}{Path.GetExtension(fileSection.FileName)}"; fileReqestPath = Path.Combine(option.RequestPath, childDir, fileName); physicalFilefilePath = Path.Combine(fileDirectory, fileName); if (File.Exists(physicalFilefilePath)) { return(false, ResultModel.Failed("你以上传过同名文件"), null); } accumulator.Append("mimeType", fileSection.Section.ContentType); accumulator.Append("fileName", fileName); accumulator.Append("filePath", fileReqestPath); //using (var writeStream = File.Create(filePath)) using (var writeStream = File.Create(physicalFilefilePath)) { const int bufferSize = 1024; await fileSection.FileStream.CopyToAsync(writeStream, bufferSize, cancellationToken); } } else { var formDataSection = section.AsFormDataSection(); var name = formDataSection.Name; var value = await formDataSection.GetValueAsync(); accumulator.Append(name, value); } section = await reader.ReadNextSectionAsync(cancellationToken); } fileInfo.LocalUrl = fileReqestPath; } catch (OperationCanceledException) { if (File.Exists(physicalFilefilePath)) { File.Delete(physicalFilefilePath); } return(false, ResultModel.Failed("用户取消操作"), null); } // Bind form data to a model var formValueProvider = new FormValueProvider( BindingSource.Form, new FormCollection(accumulator.GetResults()), CultureInfo.CurrentCulture); return(true, ResultModel.Success(fileInfo), formValueProvider); }