windows azure上传文件路径错误:'找不到文件......'

时间:2013-03-17 07:05:27

标签: azure azure-storage azure-storage-blobs

我一直在尝试将文件上传到 Azure存储容器。 当我在本地运行我的网络应用程序时,如果网站 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;
        }

2 个答案:

答案 0 :(得分:3)

HttpPostedFileBase manual有这个说法;

  

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

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

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

blockBlob.Properties.ContentType = file.ContentType;

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

答案 1 :(得分:2)

非常感谢你:Joachim Isaksson(上图)昨天我花了整整一天的时间与这一行代码进行斗争。我已经对您的答案进行了投票,但我认为我现在要在我自己的代码中添加您的解决方案的副本:

On macOS systems, verify that you have added the following paths to the
PATH environment variable.

For Genymotion earlier than 2.6:
/Applications/Genymotion.app/Contents/MacOS/
/Applications/Genymotion Shell.app/Contents/MacOS/

For Genymotion 2.6:
/Applications/Genymotion.app/Contents/MacOS/player.app/Contents/MacOS
/Applications/Genymotion Shell.app/Contents/MacOS/

For example: Run the following command 
export PATH=$PATH:/Applications/Genymotion\ Shell.app/Contents/MacOS/:/Applications/Genymotion.app/Contents/MacOS/