Asp.net mvc 4 如何在上载时捕获azure blob引用
我正在尝试设计一个简单的视图,用于将文件上载到azure存储容器中。上传后,我想捕捉上传文件的BlockBlobReference,将引用存储在数据库字段中,以便能够下载文件供以后使用。 我已经实现了上传的视图和控制器(上传工作),但我不知道如何处理其余的问题 以下是我的razor视图的一部分:Asp.net mvc 4 如何在上载时捕获azure blob引用,asp.net-mvc-4,azure,Asp.net Mvc 4,Azure,我正在尝试设计一个简单的视图,用于将文件上载到azure存储容器中。上传后,我想捕捉上传文件的BlockBlobReference,将引用存储在数据库字段中,以便能够下载文件供以后使用。 我已经实现了上传的视图和控制器(上传工作),但我不知道如何处理其余的问题 以下是我的razor视图的一部分: <div> @using (Html.BeginForm("UploadTestFile", "Upload", FormMethod.Post, new { enctype
<div>
@using (Html.BeginForm("UploadTestFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<span>Select File:</span>
<input type="file" name="loadedFiles" multiple />
<hr />
<input type="submit" value="Upload" />
<br />
<span style="color:green">@ViewBag.Message</span>
}
</div>
有人能帮我一下或给我一些提示吗。
在这方面,Manu您可以安全地只存储文件名(以及容器名称,以防其发生更改)或url(取决于容器是否为公共) 如果容器是公共的,则只需使用blob url:
https://{yourstorageaccount}.blob.core.windows.net/{container}/{filename.extension}
如果其私有,则可以存储文件名并按如下方式读取文件:
public byte[] ReadFile(string containerName, string fileName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var fileBase = container.GetBlockBlobReference(fileName);
if (!fileBase.Exists())
{
return null;
}
fileBase.FetchAttributes();
var fileByteLength = fileBase.Properties.Length;
var fileByteArray = new Byte[fileByteLength];
fileBase.DownloadToByteArray(fileByteArray, 0);
return fileByteArray;
}
或者,如果要将内容作为字符串,请执行以下操作:
public string ReadFileAsString(string containerName, string fileName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(containerName);
var fileBase = container.GetBlockBlobReference(fileName);
if (!fileBase.Exists())
{
return null;
}
return fileBase.DownloadText(Encoding.UTF8, AccessCondition.GenerateEmptyCondition(), null, null);
}
如果您想从MVC操作触发下载,可以执行以下操作:
public ActionResult GetFile(string fileName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("myContainer");
var fileBase = container.GetBlockBlobReference(fileName);
if (!fileBase.Exists())
{
return null;
}
fileBase.FetchAttributes();
var fileByteLength = fileBase.Properties.Length;
var fileByteArray = new Byte[fileByteLength];
fileBase.DownloadToByteArray(fileByteArray, 0);
return File(fileByteArray, fileBase.Properties.ContentType, fileName);
}
为什么要存储BlobReference?您总是可以只使用名称和容器名称下载文件。@Matias:谢谢您的提示。我不知道只有容器和文件名就足够了。这段代码很有用。但是关于下载:我的意思是在razor视图上显示一个下载链接,指向先前存储的文件,以便用户能够下载它。如何将url(作为变量)放入razor视图?(我自己不需要下载该文件)。控制器必须是什么样子(如果我需要的话)才能开始下载?容器是公共的吗?是的。但是后来我想对SASC做同样的事情,添加了一个下载文件的MVC操作的代码示例。您可以使用锚定标签从视图中指向它。再次感谢。这将使我在试图理解和实现它的一段时间内保持忙碌。如果我失败了,我会再次问:)
public ActionResult GetFile(string fileName)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("myContainer");
var fileBase = container.GetBlockBlobReference(fileName);
if (!fileBase.Exists())
{
return null;
}
fileBase.FetchAttributes();
var fileByteLength = fileBase.Properties.Length;
var fileByteArray = new Byte[fileByteLength];
fileBase.DownloadToByteArray(fileByteArray, 0);
return File(fileByteArray, fileBase.Properties.ContentType, fileName);
}