简体   繁体   English

如何在Asp.Net Core中将文件上传到Azure Blob?

[英]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 我尝试使用以下代码将文件上传到我的azure blob中

  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. 实际上,我已经上传了jpg文件,但它以“应用程序/八位字节码”类型上传。 how to resolve it? 怎么解决呢?

And my scenario is while uploading the file, windows explorer will open to select the file to upload. 我的情况是上传文件时,Windows资源管理器将打开以选择要上传的文件。 So if we provide the path as static as below, 因此,如果我们提供的静态路径如下所示,

newBlob.UploadFromFileAsync( @"C:\\Users\\joy\\Downloads\\" + file.FileName ); 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 尝试使用UploadFromStream,让我知道结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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