簡體   English   中英

如何在Asp.Net Core中將文件上傳到Azure Blob?

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

我嘗試使用以下代碼將文件上傳到我的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;}

        }

實際上,我已經上傳了jpg文件,但它以“應用程序/八位字節碼”類型上傳。 怎么解決呢?

我的情況是上傳文件時,Windows資源管理器將打開以選擇要上傳的文件。 因此,如果我們提供的靜態路徑如下所示,

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

它不適用於申請。 如何更改此代碼以從各個位置上傳文件?

嘗試使用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/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