[英]Upload file into Blob Azure
我正在尝试从MVC应用程序上传文件到blob Azure,我正在按照本教程,我陷入了这个片段:
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
blockBlob.UploadFromStream(fileStream);
}
如何使@"path\\myfile"
动态? 如何检索我的文件的真实路径放在那里? 我也接受任何其他建议上传到blob :)
UPDATE
建议我在我的视图中添加代码:
@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>
完整的动作控制器:
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
属性将为您提供流
[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存储中,首先获取其容器名称
//StorageConnectionString string specified in configuration file
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve a reference to a container.
//specified container name
CloudBlobContainer container = blobClient.GetContainerReference("myBlobcontainer");
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myBlob");
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = file.InputStream)
{
blockBlob.UploadFromStream(fileStream);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.