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

Day*_*ker 3 c# azure azure-blob-storage asp.net-core

我想将图像文件上传到 Azure Blob 容器。我正在使用.net core 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());
}
Run Code Online (Sandbox Code Playgroud)

谁能帮助我如何上传保持原始内容类型的图像。

提前致谢。

Jim*_* Xu 5

如果您想在上传文件到Azure blob时设置content-type,请参考以下代码

// 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";
}
Run Code Online (Sandbox Code Playgroud)

测试(我在Postman中测试) 在此输入图像描述 在此输入图像描述