0
votes

I am trying to upload a word document to blob storage using C#. The code snippet is as below:

var blobServiceClient = new BlobServiceClient(connectionString);
var containerClient = blobServiceClient.GetBlobContainerClient(container);
containerClient.CreateIfNotExists(PublicAccessType.None);
var blobClient = containerClient.GetBlobClient(documentName);
using (var memoryStream = new MemoryStream({Binary of the File}))
{
    var headers = new BlobHttpHeaders() { ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" };
    await blobClient.UploadAsync(memoryStream, httpHeaders: headers).ConfigureAwait(false);
}

The document gets uploaded successfully to the Blob Storage. I can see that the content type is also being set properly while looking through Azure Storage Explorer. But, when i try to access this document (using document URL) through browser (chrome), it downloads the file as unknown file.

I tried the same by uploading a word document through Azure Storage Explorer. This file gets downloaded as word document while downloading it through browser.

Any idea what is going wrong?

1
Will it be possible to share the actual blob URL?Gaurav Mantri
did you try other browser, EDGE or IE?Leon Yue

1 Answers

0
votes

As you see the figure below, the Content-Type header is stored as a property in the Blob Properties, so it will be set into the headers of the download response and then it will be recognized as a word file while download by browser.

enter image description here

So when you upload a word file, it's necessary to set the ContentType property for a blob via BlobBaseClient.SetHttpHeaders(BlobHttpHeaders, BlobRequestConditions, CancellationToken) Method or BlobBaseClient.SetHttpHeadersAsync(BlobHttpHeaders, BlobRequestConditions, CancellationToken) Method.

And then to write the Content-Typeheader of the headers of a download response with the value of contentType below by yourself on your server app.

Response<BlobProperties> response = await blobClient.GetPropertiesAsync();
var contentType = response.Value.ContentType

Or the download response from a blob url with sas token which headers default include the ContentType property as the Content-Type header.