将文件上传到Blob Azure [英] Upload file into Blob Azure
本文介绍了将文件上传到Blob Azure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将文件从MVC应用程序上传到blob Azure中,我在关注
I'm trying to upload files into blob Azure from an MVC app, I'm following this tutorial, and I'm stuck in this snippet:
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
blockBlob.UploadFromStream(fileStream);
}
如何使@"path\myfile"
动态?如何检索文件的真实路径以将其放入其中?我也接受任何其他建议以上传到blob:)
How to make @"path\myfile"
dynamic?? How to retrieve the real path of my file to put it in there? I accept any other suggestion too to upload to blob :)
更新
根据建议,我在视图中添加了代码:
As suggested I added the code inside my View:
@model RiPSShared.Models.RiPSModels.AgencyNote
<h2>PostFile</h2>
<form action="@Url.Action("PostFile", "AgencyNotes", new { NoteId=Model.aut_id})" method = "post" enctype="multipart/form-data">
<label for="file1"> File name:</label>
<input type="file" name="file" id="file1" />
<input type="submit" value="Submit" />
</form>
以及完整的动作控制器:
And the full action controller:
public ActionResult PostFile(HttpPostedFileBase file, int NoteId)
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference(path);
using (Stream fileStream = file.InputStream)
{
blockBlob.UploadFromStream(fileStream);
}
return RedirectToAction("Index");
}
推荐答案
HttpPostedFileBase
将提供您所需的信息.具体来说,InputStream
属性将为您提供视频流
HttpPostedFileBase
will have the information you need. Specifically the InputStream
property will give you the stream
[HttpPost]
public ActionResult PostFile(HttpPostedFileBase file, int NoteId)
{
// Your existing code for azure blob access
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
// make sure to a null check on file parameter before accessing the InputStream
using (var s = file.InputStream)
{
blockBlob.UploadFromStream(s);
}
// to do :Return something
}
这篇关于将文件上传到Blob Azure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文