如何从MVC视图将文件上载到Azure blob存储

时间:2014-09-17 11:23:01

标签: azure file-upload asp.net-mvc-5 azure-storage-blobs asp.net-mvc-views

我正在编写一个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 View调用UploadTestFile()函数,用户可以浏览到要上传的文件。

我是否需要使用Ajax,或者我可以通过从MVC视图调用方法来上传文件?我能帮忙解决一下吗?

提前致谢

1 个答案:

答案 0 :(得分:9)

从MVC视图调用UploadTestFile()函数的一种方法是使用Html.BeginForm()方法。我在下面举了一个例子:

@using (Html.BeginForm("UploadTestFile", "INSERT_YOUR_CONTROLLER_NAME_HERE", FormMethod.Post, new { enctype = "multipart/form-data" })) {
    <span>
        <input type="file" name="myFile" multiple /> <br>
        <input type="submit" value="Upload" />
    </span>

}

另外,对您的代码提出了几点建议:

  1. UploadFileToBlobStorage():代码检查容器是否存在以及对每个请求设置权限。我建议将container.CreateIfNotExists()和container.SetPermissions(...)逻辑分离为一个单独的初始化函数,该函数在首次部署时只需要执行一次。

  2. UploadFileToBlobStorage():看起来代码会尝试从VM文件系统上传localFileName而不是多部分表单数据。一种方法是使用HttpFileCollectionBase类和Controller.Request属性。示例如下:

    public void UploadFileToBlobStorage(
        string containerName, 
        string blockBlogName, 
        HttpFileCollectionBase files) 
    {
    
        // .....
    
        // Use this:
        blockBlob.UploadFromStream(files[0].InputStream); 
    
        /* uploading the first file: 
           you can enumerate thru the files collection 
           if you are uploading multiple files */
    
        /* Instead of this: 
           Create or overwrite the "myblob" blob with contents 
           from a local file. */
        using (var fileStream = System.IO.File.OpenRead(fileName)) 
        {
            blockBlob.UploadFromStream(fileStream);
        }
    }
    
    [HttpPost]
    public void UploadTestFile() 
    {
        string containerName = "TestContainer";
        string blockBlogName = "Test.txt";
        AzureService azureService = new AzureService();
    
        // Notice the Request.Files instead of localFileName
        azureService.UploadFileToBlobStorage(
              containerName, blockBlogName, Request.Files);
    }
    
  3. 请告诉我这是否适用于你。