简体   繁体   中英

How to upload a file in to azure blob in Asp.Net Core?

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?

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://docs.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM