7
在MVC 6中调整上传图像的最佳方式是什么?我想存储图像的多个变体(如小,大等),以便能够选择稍后显示的图像。在MVC 6中调整上传图像的大小6
这是我的行为代码。
[HttpPost]
public async Task<IActionResult> UploadPhoto()
{
if (Request.Form.Files.Count != 1)
return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
IFormFile file = Request.Form.Files[0];
// calculate hash
var sha = System.Security.Cryptography.SHA256.Create();
byte[] hash = sha.ComputeHash(file.OpenReadStream());
// calculate name and patch where to store the file
string extention = ExtentionFromContentType(file.ContentType);
if (String.IsNullOrEmpty(extention))
return HttpBadRequest("File type not supported");
string name = WebEncoders.Base64UrlEncode(hash) + extention;
string path = "uploads/photo/" + name;
// save the file
await file.SaveAsAsync(this.HostingEnvironment.MapPath(path));
}
谢谢!正是我需要的! –
@Phill伟大的链接,谢谢,我发现了唯一的vNext图像。但是“新大小”是System.Drawing部分吗? – Alex
@voo当前的Nuget仍然需要System.Drawing,但API正在改变V2来放弃这个约束。 https://github.com/JimBobSquarePants/ImageProcessor#api-changes – Phill