How to upload a file directly to azure blob from the URL in c#?

I have a url which looks like following “” which contains the file. “UploadedZIPFile” is the file Name. In URL I don’t have the extension of filename. After downloading the file into my system it is showing the extension. I want to upload the file which came from the URL into Azure blob storage. How can I upload the file to azure blob storage from the given URL using c#? Please Added my code sample below

public async Task<string> automaticFileUpload(string ticketNum, string type, string fileURL) 
            Uri uri = new Uri(fileURL);
            string fileName = System.IO.Path.GetFileName(uri.LocalPath);
            string extension = ".zip";
            string finalFileName = fileName + extension;


            var connectionString = "xxxxxxx";

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer =
            await cloudBlobContainer.CreateIfNotExistsAsync();
            BlobContainerPermissions permissions = new BlobContainerPermissions
                PublicAccess = BlobContainerPublicAccessType.Blob
            await cloudBlobContainer.SetPermissionsAsync(permissions);
            if (!cloudBlobContainer.GetPermissionsAsync().Equals(BlobContainerPublicAccessType.Off))
                await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions
                    PublicAccess = BlobContainerPublicAccessType.Off

            CloudBlockBlob blockblob = cloudBlobContainer.GetBlockBlobReference(finalFileName);
            blockblob.Properties.ContentType = "application/zip";

            using (var client = new HttpClient())
                //get the url 
                var request = new HttpRequestMessage(HttpMethod.Get, fileURL);
                var sendTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
                var response = sendTask.Result;

                HttpStatusCode status = response.StatusCode;
                if (status == HttpStatusCode.OK)
                    var httpStream = response.Content.ReadAsStreamAsync().Result;
                    MemoryStream ms = new MemoryStream();
                    ms.Position = 0;
                    await blockblob.UploadFromStreamAsync(ms);
                    return blockblob.Uri.AbsoluteUri;

            return null;

As of now I am taking hard coded values for file extension and content type But I need dynamic values

Thanks in Advance


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could get the content-type in the response headers, see:

How to upload a file directly to azure blob from the URL in c#?

And then get file extension from content type in this way.

There is the code for getting the headers with HttpWebResponse. For more details, see the document.

// Creates an HttpWebRequest for the specified URL.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
// Sends the HttpWebRequest and waits for response.
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

// Displays all the headers present in the response received from the URI.
Console.WriteLine("rnThe following headers were received in the response:");
// Displays each header and it's key associated with the response.
for(int i=0; i < myHttpWebResponse.Headers.Count; ++i)
    Console.WriteLine("nHeader Name:{0}, Value :{1}",myHttpWebResponse.Headers.Keys[i],myHttpWebResponse.Headers[i]);
// Releases the resources of the response.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x