public IActionResult UploadFormFile(IFormFile file) { if (file == null) { throw new HttpBadRequestException("请将Content-Type设置application/x-www-form-urlencoded,然后在formData的file附上图标文件"); } string iconExt = ""; Microsoft.Extensions.Primitives.StringValues headerVar; Request.Headers.TryGetValue("fileExt", out headerVar); if (headerVar.Count > 0) { iconExt = headerVar[0].Trim(); } if (string.IsNullOrWhiteSpace(iconExt)) { throw new HttpBadRequestException("请在Headers上添加fileExt标识icon的扩展名"); } iconExt = iconExt.Replace(".", string.Empty); //先把文件保存到临时文件夹,计算md5 var tmpPath = Path.Combine(tmpFileFolder, $"{Guid.NewGuid()}.{iconExt}"); using (FileStream fs = System.IO.File.Create(tmpPath)) { file.CopyTo(fs); // 清空缓冲区数据 fs.Flush(); } var md5 = MD5Gen.CalcFile(tmpPath); var fileName = $"{md5}.{iconExt}"; var iconPath = Path.Combine(iconFolder, fileName); //如果文件不存在资源文件夹,拷贝存储 if (!System.IO.File.Exists(iconPath)) { System.IO.File.Copy(tmpPath, iconPath); } //为文件生成缩略图标 ImageThumbnailCreator.SaveImageThumbnails(iconPath); //删除临时文件 if (System.IO.File.Exists(tmpPath)) { System.IO.File.Delete(tmpPath); } return(Ok($"/{OSSConst.AppRouteArea}/{OSSConst.IconFolder}/{fileName}")); }
public async Task <IActionResult> UploadFormFile(IFormFile file) { if (file == null) { return(BadRequest()); } var command = new FileCreateCommand(Request.Headers); //先把文件保存到临时文件夹,计算md5 var tmpPath = Path.Combine(tmpFileFolder, $"{Guid.NewGuid()}.{command.FileExt}"); using (FileStream fs = System.IO.File.Create(tmpPath)) { file.CopyTo(fs); // 清空缓冲区数据 fs.Flush(); } var finfo = new FileInfo(tmpPath); command.MD5 = MD5Gen.CalcFile(tmpPath); command.Size = finfo.Length; var filePath = Path.Combine(clientAssetFolder, $"{command.MD5}.{command.FileExt}"); //如果文件不存在资源文件夹,拷贝存储 if (!System.IO.File.Exists(filePath)) { System.IO.File.Copy(tmpPath, filePath); } //删除临时文件 if (System.IO.File.Exists(tmpPath)) { System.IO.File.Delete(tmpPath); } if (string.IsNullOrWhiteSpace(command.Name)) { command.Name = command.MD5; } command.Size = new FileInfo(filePath).Length; var id = await _mediator.Send(command); return(await Get(id)); }
public IActionResult UploadStreamFile() { string assetExt = ""; Microsoft.Extensions.Primitives.StringValues headerVar; Request.Headers.TryGetValue("fileExt", out headerVar); if (headerVar.Count > 0) { assetExt = headerVar[0].Trim(); } if (string.IsNullOrWhiteSpace(assetExt)) { throw new HttpBadRequestException("请在Headers上添加fileExt标识Asset的扩展名"); } assetExt = assetExt.Replace(".", string.Empty); //先把文件保存到临时文件夹,计算md5 var tmpPath = Path.Combine(tmpFileFolder, $"{Guid.NewGuid()}.{assetExt}"); using (FileStream fs = System.IO.File.Create(tmpPath)) { HttpContext.Request.Body.CopyTo(fs); // 清空缓冲区数据 fs.Flush(); } var md5 = MD5Gen.CalcFile(tmpPath); var fileName = $"{md5}.{assetExt}"; var iconPath = Path.Combine(srcClientAssetFolder, fileName); //如果文件不存在资源文件夹,拷贝存储 if (!System.IO.File.Exists(iconPath)) { System.IO.File.Copy(tmpPath, iconPath); } //删除临时文件 if (System.IO.File.Exists(tmpPath)) { System.IO.File.Delete(tmpPath); } return(Ok($"/{OSSConst.AppRouteArea}/{OSSConst.SrcClientAssetFolder}/{fileName}")); }