【问题标题】:windows azure upload file path error: 'Could not find file... 'windows azure上传文件路径错误:'找不到文件...'
【发布时间】:2017-03-30 19:50:42
【问题描述】:

我一直在尝试将文件上传到 Azure 存储容器。 当我在本地运行我的网络应用程序时,如果文件 not 在网站 root 中,我会收到 错误

Could not find file 'C:\Program Files (x86)\IIS Express\test.txt'.

如果我将文件复制网络应用根文件夹,它可以正常工作。

上运行网络应用我收到一个错误

Could not find file 'D:\Windows\system32\test.txt'.

我无法从 HttpPostedFileBase 对象获取完整的本地文件路径

代码

private string UploadFile(HttpPostedFileBase file, string folder)
    {
        try
        {
            var date = DateTime.UtcNow.ToString("yyyyMMdd-hhmmss-");
            var fileName = Path.GetFileName(file.FileName);

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=test;AccountKey=asdfasfasdasdf");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
            CloudBlobContainer container = blobClient.GetContainerReference(folder);
            bool b = container.CreateIfNotExists();

            CloudBlockBlob blockBlob = container.GetBlockBlobReference(date + fileName);
            blockBlob.Properties.ContentType = file.ContentType;

            using (var fileStream = System.IO.File.OpenRead(fileName))
            {
                blockBlob.UploadFromStream(fileStream);
            }
            return blockBlob.Uri.AbsoluteUri;

        }
        catch (Exception ex)
        {
            return ex.Message;
        }

【问题讨论】:

    标签: azure azure-storage azure-blob-storage


    【解决方案1】:

    HttpPostedFileBase manual 有话要说;

    FileName        获取客户端上文件的完全限定名。

    也就是文件名不是可以在服务器上打开的。

    我认为你真正想做的是使用InputStream property;

    blockBlob.Properties.ContentType = file.ContentType;
    
    blockBlob.UploadFromStream(file.InputStream);  // Upload from InputStream
    return blockBlob.Uri.AbsoluteUri;
    

    【讨论】:

      【解决方案2】:

      非常感谢:Joachim Isaksson(上图)昨天我花了一整天的时间与这一行代码作斗争。我已经投票赞成你的答案,但我想我现在应该在我自己的代码中添加一份你的解决方案:

      public async Task<ActionResult> UploadAsync()
      {
          try
          {
              HttpFileCollectionBase files = Request.Files;
              int fileCount = files.Count;
      
              if (fileCount > 0)
              {
                  for (int i = 0; i < fileCount; i++)
                  {
                      CloudBlockBlob blob = blobContainer.GetBlockBlobReference(GetRandomBlobName(files[i].FileName));
      
                      blob.Properties.ContentType = files[i].ContentType;
                      blob.UploadFromStream(files[i].InputStream);
      
                      // the above 2 lines replace the following line from the downloaded example project:
                      //await blob.UploadFromFileAsync(Path.GetFullPath(files[i].FileName), FileMode.Open);
                  }
              }
              return RedirectToAction("Index");
          }
      }
      

      【讨论】:

      • 谢谢你帮了我很多,但可以通过使用await blob.UploadFromStreamAsync...而不是blob.UploadFromStream...来改进
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      相关资源
      最近更新 更多