【问题标题】:How do I upload a file to Azure blob storage from a MVC view如何从 MVC 视图将文件上传到 Azure Blob 存储
【发布时间】: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


    【解决方案1】:

    从 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-11-22
      • 2013-07-29
      • 2019-08-13
      • 2017-09-21
      • 2021-03-07
      • 2017-01-24
      • 2017-08-19
      • 2015-05-20
      相关资源
      最近更新 更多