将IFormFile转换为Stream将内容类型转换为application / octet-stream

时间:2020-04-19 08:04:00

标签: c# azure asp.net-core azure-blob-storage

我想将图像文件上传到Azure Blob容器。我正在使用.net核心webapi post方法上传图像。上传成功,但是内容类型无效,将原始image/jpeg类型转换为application/octet-stream

[HttpPost]
public async Task<string> Post(IFormFile files)
{
    BlobClient blobClient = _containerClient.GetBlobClient(files.FileName);
    await blobClient.UploadAsync(files.OpenReadStream());
}

任何人都可以帮助我如何上传保留原始内容类型的图像。

谢谢。

1 个答案:

答案 0 :(得分:2)

如果要在将文件上传到Azure blob时设置内容类型,请参考以下代码

// I use the sdk Azure.Storage.Blobs
[HttpPost]
public async Task<string> Post(IFormFile file)
{
    var connectionString = "the account connection string";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            BlobContainerClient containerClient =blobServiceClient.GetBlobContainerClient("test");
            await containerClient.CreateIfNotExistsAsync();
            BlobClient blobClient = containerClient.GetBlobClient(file.FileName);
            BlobHttpHeaders httpHeaders = new BlobHttpHeaders() { 
               ContentType=file.ContentType 
            };

            await blobClient.UploadAsync(file.OpenReadStream(), httpHeaders);

            return "OK";
}

测试(我在邮递员中测试) enter image description here enter image description here