提问者:小点点

无法从我的表单将文件上载到Azure Blob存储


我需要从前端的窗体上传一个文件,通过后端进入Azure Blob存储。

  1. 我需要保存在blob上,并将url保存在azure blob存储上
  2. 我使用一个类来解析数据,从控制器上的表单接收文件
  3. 控制器处理文件并连接azure blob存储
  4. blob存储在文档中没有关于其方法的示例
  5. 我找到的所有示例都只是演示如何从本地文件夹上载

在代码之后,到目前为止的问题是myFile var的格式正在传递给方法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);
         }

我收到一条消息:等待cloudBlockBlob。UploadFromStreamAsync(myFile)它告诉我文件

无法将HttpPostedFileBase转换为System.IO.流


共1个答案

匿名用户

嗯,我说如果有东西想要一个流,给它一个流:

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