我需要从前端的表单上载文件,并通过后端上传到azure blob存储。
遵循代码,到目前为止,问题是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);
}
我在await cloudBlockBlob.UploadFromStreamAsync(myFile);
上收到一条消息,告诉我该文件
HttpPostedFileBase无法转换为System.IO.Stream
答案 0 :(得分:1)
好吧,我说如果某事想要流,请给它一个流:
using (Stream stream = myFile.OpenReadStream())
{
await cloudBlockBlob.UploadFromStreamAsync(stream);
}