6

I just started the Quickstart with .NET v12 SDK https://learn.microsoft.com/da-dk/azure/storage/blobs/storage-quickstart-blobs-dotnet

But i cant seem to find out how to specify a ContentType when uploading a blob.

Does anyone know that?

Thanks in advance.

2 Answers 2

18

You can set it as follows,

 await blobClient.UploadAsync(stream, true, default);
 await blobClient.SetHttpHeadersAsync(new BlobHttpHeaders
 {
    ContentType = contentType
 });

EDIT: As mentioned in the comment, the efficient way to do the same with a single method as follows,

await blobClient.UploadAsync(ms, new BlobHttpHeaders{ ContentType = "text/plain"});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you so much Sajeetharan! I also just found out that you can upload the blob and specify ContentType with one single call "await blobClient.UploadAsync(ms, new BlobHttpHeaders{ ContentType = "text/plain" });"
6

Please try this override of UploadAsync method. This method will upload and set the content type in a single network call.

Here would be your code:

var httpHeaders = new BlobHttpHeaders
 {
    ContentType = contentType
 });
await blobClient.UploadAsync(stream, httpHeaders);

2 Comments

With this override of UploadAsync, it's also possible to set the metadata for the blob.
@AmélieDupré - Overload takes an instance of BlobUploadOptions, which facilitates Metadata. See: learn.microsoft.com/en-us/dotnet/api/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.