【发布时间】:2014-11-11 09:50:04
【问题描述】:
我正在编写一个 MVC5 互联网应用程序,并希望获得一些帮助,以便将文件从我自己的文件系统上传到 Azure Blob。
这是我的 Azure 上传代码功能:
public void UploadFileToBlobStorage(string containerName, string blockBlogName, string fileName)
{
// 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
});
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blockBlogName);
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(fileName))
{
blockBlob.UploadFromStream(fileStream);
}
}
这是我上传测试文件的功能:
public void UploadTestFile(string localFileName)
{
string containerName = "TestContainer";
string blockBlogName = "Test.txt";
AzureService azureService = new AzureService();
azureService.UploadFileToBlobStorage(containerName, blockBlogName, localFileName);
}
我不确定如何从 MVC 视图调用 UploadTestFile() 函数,用户可以在其中浏览到要上传的文件。
我需要使用 Ajax,还是可以简单地通过从 MVC 视图调用方法来上传文件?我可以帮忙吗?
提前致谢
【问题讨论】:
标签: azure file-upload asp.net-mvc-5 azure-blob-storage asp.net-mvc-views