【问题标题】:ASP.NET MVC Core Azure Blob Image ResizerASP.NET MVC Core Azure Blob Image Resizer
【发布时间】:2018-11-20 11:24:49
【问题描述】:

我有一个 ASP.NET 核心应用程序,可以将图像上传到 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));
}

【问题讨论】:

  • 你有什么问题?

标签: azure asp.net-core asp.net-core-mvc azure-blob-storage imageresizer


【解决方案1】:

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

示例(伪):

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

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

【讨论】:

  • 这真的有效!它成功调整了我的图像大小,并且能够将其上传到 azure。我不完全了解 MemoryStream 的工作原理,但从我收集的信息来看,图像并未存储在我的硬盘驱动器中,而是为图像分配了内存。图片上传后会立即销毁吗?还是会无限期地占用内存?
  • 它会被释放,因为它在使用中分配。很高兴为您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多