【问题标题】:can not able to set Content type无法设置内容类型
【发布时间】:2019-08-05 06:40:39
【问题描述】:

我正在尝试将图像保存在 azure blob 存储中。我通过代码设置了内容类型,如下但它正在准备但没有上传。我该怎么办?

文件上传代码:

[HttpPost("ListFiles")]
public async Task<List<string>> InsertFile(List<IFormFile> asset)
{
    try
    {
        var urlList = new List<string>();
        foreach (var op in asset)
        {


            if (CloudStorageAccount.TryParse(_config.Value.StorageConnection, out CloudStorageAccount storageAccount))
            {
                CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                CloudBlobContainer container = blobClient.GetContainerReference(_config.Value.Container);

                  var postedFileExtension = Path.GetExtension(op.FileName);
                var img = $@"{Guid.NewGuid()}" + postedFileExtension;
                CloudBlockBlob blockBlob = container.GetBlockBlobReference(img);
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(img);
                cloudBlockBlob.Properties.ContentType = op.ContentType;
                await blockBlob.UploadFromStreamAsync(op.OpenReadStream());
                var blob = container.GetBlockBlobReference(img);

                urlList.Add(blob.Uri.AbsoluteUri);
            }

        }
        return urlList;
    }
    catch (Exception e)
    {
        throw e;
    }
}

【问题讨论】:

  • 您正在更新记录的第二个引用的属性,但不保存更改。重用blockBlob 并将其设置为ContentType 的值或保存cloudBlockBlob

标签: azure azure-blob-storage


【解决方案1】:

要在上传 blob 时设置内容类型,只需在调用上传方法之前设置 ContentType 属性即可。下面是我的测试代码,你可以试试。

static void Main(string[] args)
    {
        String strorageconn = "storage string";
        CloudStorageAccount storageacc = CloudStorageAccount.Parse(strorageconn);

        //Create Reference to Azure Blob
        CloudBlobClient blobClient = storageacc.CreateCloudBlobClient();

        CloudBlobContainer container = blobClient.GetContainerReference("test");

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("test.jpg");

        blockBlob.Properties.ContentType = "image/jpg";

        using (var filestream = System.IO.File.OpenRead(@"D:\Picture\test.jpg"))
        {

            blockBlob.UploadFromStream(filestream);

        }
    }

并使用存储资源管理器读取属性,结果如下。

【讨论】:

    【解决方案2】:

    你正在使用

    var postedFileExtension = Path.GetExtension(op.FileName);
    var img = $@"{Guid.NewGuid()}" + postedFileExtension;
    
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(img);
    CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(img);
    cloudBlockBlob.Properties.ContentType = op.ContentType;
    
    await blockBlob.UploadFromStreamAsync(op.OpenReadStream());
    var blob = container.GetBlockBlobReference(img);
    

    您正在设置变量的 ContentType 属性,但未将此更改提交给 Azure。将代码更新为:

    var postedFileExtension = Path.GetExtension(op.FileName);
    var img = $@"{Guid.NewGuid()}" + postedFileExtension;
    
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(img);  
    blockBlob.Properties.ContentType = op.ContentType;
    
    await blockBlob.UploadFromStreamAsync(op.OpenReadStream());
    var blob = container.GetBlockBlobReference(img);
    
    Console.WriteLine(blob.Properties.ContentType); // Output the contentType
    

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 2019-03-28
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2012-12-30
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多