0

I have tried to upload the files in to my azure blob using below code

  public async void UploadSync(IEnumerable<IFormFile> files, string path)
        {
            string MyPath = path.Replace("https://browsercontent.blob.core.windows.net/blob1/", "");

            try
            {
                foreach (var file in files)
                {
                    var newBlob = container.GetBlockBlobReference(MyPath);
                    await newBlob.UploadFromFileAsync(@"C:\Users\joy\Downloads\" + file.FileName);

                }
            }
            catch (Exception ex)
            { throw ex;}

        }

Actually i have upload the jpg file but it upload in a "application/octact steam" type. how to resolve it?

And my scenario is while uploading the file, windows explorer will open to select the file to upload. So if we provide the path as static as below,

newBlob.UploadFromFileAsync(@"C:\Users\joy\Downloads\" + file.FileName);

it will not be applicable for application. How to change this code to upload the files from various locations?

1 Answer 1

3

Try to use UploadFromStream and let me know the outcome

// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
} 

https://learn.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs

Sign up to request clarification or add additional context in comments.

2 Comments

You could update your answer to taken into consideration OPs concern around "application/octact steam" content type by including setting the blockblob.Properties.ContentType property
@sumanth, Your code works great. Thanks. Similarly how can i download the files from the azure blob storage?Can you please share the code block for downloading the files from azure blob in asp.net core

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.