簡體   English   中英

無法從我的表單將文件上傳到 Azure Blob 存儲

[英]Can´t upload file to Azure Blob Storage from my form

我需要從前端的表單上傳文件,通過后端進入 azure blob 存儲。

  1. 我需要保存在 blob 上,並將 url 保存在 azure blob 存儲中。
  2. 我正在使用類來解析數據從控制器上的表單接收文件。
  3. 控制器處理文件並連接到 azure blob 存儲。
  4. blob 存儲沒有關於他的文檔方法的示例。
  5. 我發現的所有示例都展示了如何從本地文件夾上傳。

按照代碼,到目前為止的問題是 myFile 變量的格式在方法 UploadFromStreamAsync 上傳遞。

 // the controller starts here
  [HttpPost]
         public async System.Threading.Tasks.Task<ActionResult> Index(IFormFile arquivo)
         { //here I am receving the file
             var myFile = arquivo.FileItem;
             var myFileName = arquivo.FileName;

 // here is the connection with blob account 
             string storageConnection = ConfigurationManager.AppSettings["BlobStorageString"];
             string accountBlob = ConfigurationManager.AppSettings["BlobStorageAccount"];

             var storageAccount = CloudStorageAccount.Parse(storageConnection);
             var cloudBlobClient = storageAccount.CreateCloudBlobClient();

             CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("hurrblobstest");
             await cloudBlobContainer.CreateAsync();

             var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference("filname12");

 // here I am trying to send the file
             await cloudBlockBlob.UploadFromStreamAsync(myFile);
             var result = JsonConvert.SerializeObject(cloudBlockBlob);

 //here I need to return the url or the object with the file info on the azure blob storage
             return Json(result);
         }

我收到一條消息: await cloudBlockBlob.UploadFromStreamAsync(myFile); 這告訴我該文件

HttpPostedFileBase 無法轉換為 System.IO.Stream

好吧,我說如果有東西想要一個流,給它一個流:

using (Stream stream = myFile.OpenReadStream())
{
    await cloudBlockBlob.UploadFromStreamAsync(stream);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM