1
votes

I want to upload image files to a Azure Blob Container. I am using .net core webapi post method to upload the image.The upload getting success but the content type is invalid, which convert the original image/jpeg type to application/octet-stream.

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

Can anyone help me how to upload the image keeping the original content type.

Thanks in advance.

1
I remember seeing a question some time ago which talked about getting content type of a file using file name (and extension) in asp.net core (not able to find it now, sorry). Please look at that. Once you have that, please see this: stackoverflow.com/questions/59945376/…. - Gaurav Mantri
Actually found a separate link for mime type: dotnetcoretutorials.com/2018/08/14/…. HTH. - Gaurav Mantri
@GauravMantri I can get the original mime type by using IFormFile. But here the issue is when i upload it to azure blob storage, there it show as application/octet-stream - Daybreaker
In that case check out this link: stackoverflow.com/questions/59945376/…. - Gaurav Mantri

1 Answers

3
votes

If you want to set content-type when you upload file to Azure blob, please refer to the following code

// 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";
}

Test(I test in Postman) enter image description here enter image description here