我正在尝试设计一个简单的视图,用于将文件上传到azure存储容器中。上传后,我想捕获上传文件的BlockBlobReference,将引用存储在数据库字段中,以便能够下载该文件供以后使用。 我已经意识到用于上传的视图和控制器(上传工作),但我不知道如何解决其余问题。
这是我的剃刀观点的一部分:
<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>
这是控制器和方法:
[HttpPost]
public void UploadTestFile(HttpPostedFileBase loadedFiles)
{
string containerName = "testcontainer";
UploadFileToBlobStorage(containerName, loadedFiles);
}
public void UploadFileToBlobStorage(string containerName, HttpPostedFileBase loadedFiles)
{
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
container.SetPermissions(
new BlobContainerPermissions
{
PublicAccess =
BlobContainerPublicAccessType.Blob
});
// here I would like to iterate through the items, in case that more than one file was selected
// I don't know how to iterate through loadedFiles though
// filename
string _fileName = loadedFiles.FileName;
// Retrieve reference to a blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference(_fileName);
blockBlob.UploadFromStream(loadedFiles.InputStream);
}
请有人帮助我或给我一些提示。 此致,Manu
答案 0 :(得分:1)
您可以安全地存储文件名(以及容器名称,以防它可以更改)或url(取决于容器是否公开)。
如果容器是公开的,您只需使用blob网址: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 Action触发下载,可以执行以下操作:
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);
}