5

我正在编写一个 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 视图调用该方法来简单地上传文件?我可以帮忙吗?

提前致谢

4

1 回答 1

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);
    }
    

请让我知道这是否适用于您。

于 2014-09-19T03:32:22.193 回答