繁体   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