gpt4 book ai didi

azure - ASP.NET MVC Core Azure Blob 图像调整器

转载 作者:行者123 更新时间:2023-12-02 23:45:30 25 4
gpt4 key购买 nike

我有一个 ASP.NET core 应用程序,可以将图像上传到 Azure。在将图像上传到 Azure Blob 容器之前,我尝试使用 Magick.NET 调整图像大小。到目前为止,我已成功将图像保存到本地硬盘驱动器中的文件夹中。这是正确的写法吗?

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Product products)
{
var files = products.UploudThumbnail;

List<string> image = new List<string>();
List<string> names = new List<string>();

if (files != null)
{
foreach (var file in files)
{
if (ModelState.IsValid)
{
if (file.ContentType == "image/jpeg" || file.ContentType == "image/jpg")
{
if (file.Length < 1 * 1000 * 1000)
{
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

var fileName = parsedContentDisposition.FileName.Trim('"');
names.Add(fileName);

fileName = Guid.NewGuid().ToString() + "-" + fileName;

CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
cloudBlockBlob.Properties.ContentType = file.ContentType;
await cloudBlockBlob.UploadFromStreamAsync(file.OpenReadStream());

image.Add(cloudBlockBlob.Uri.AbsoluteUri);


const int size = 20;
const int quality = 75;

using (var image = new MagickImage(file.OpenReadStream()))
{
image.Resize(size, size);
image.Strip();
image.Quality = quality;
//how to save it into azure?
image.Write(fileName);
}

}
else
{
ModelState.AddModelError("UploudThumbnail", "Max size not accepted");
}
}
else
{
ModelState.AddModelError("UploudThumbnail", "jpeg & jpg are accepted");
}

}
}
}

_context.Add(products);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}

最佳答案

只需将图像写入内存流并使用UploadFromStreamAsync上传即可。方法。

示例(伪):

using (var memStream = new MemoryStream())
{
image.Write(memStream);

memStream.Seek(0, SeekOrigin.Begin);
await cloudBlockBlob.UploadFromStreamAsync(memStream);
}

关于azure - ASP.NET MVC Core Azure Blob 图像调整器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50793859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com