给定路径格式不受支持(Visual Studio / Azure)?

时间:2018-03-27 19:19:49

标签: c# visual-studio azure path azure-storage

当我点击下载链接时,它会将我发送到一个错误页面,试图调试它告诉我不支持我的给定路径格式

在我的控制器类中:

        public async Task<ActionResult> DownloadBlob(string file, string extension)
    {
        string downloadPath = await repo.DownloadBlobAsync(file, extension);
        return Json(downloadPath);
    }

在我的Blob存储类中:

        public async Task<string> DownloadBlobAsync (string file, string fileExtension)
    {
        _cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
        CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);

        var path = downloadPath + file + "." + fileExtension;
        using (var fileStream = System.IO.File.OpenWrite(path))
        {
            fileStream.Position = 1;
            //fileStream.Seek(0, SeekOrigin.Begin);

            await blockBlob.DownloadToStreamAsync(fileStream);

            return path;
        }
    }

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NotSupportedException:不支持给定路径的格式

错误来源:

using (var fileStream = System.IO.File.OpenWrite(path))

1 个答案:

答案 0 :(得分:0)

根据您的说明,我建议您设置断点以检查 downloadPath 路径的值#39;字段。

enter image description here

我创建了一个简单的演示,可以将blob文件下载到本地文件夹路径。它工作正常。你可以参考。

string containerNamex = "container002";
string file = "MyTest"; //a blob file named MyTest.txt
string fileExtension = "txt";
string downloadPath = @"D:\MyBlob\";
CloudBlobClient  _cloudBlobClientx = storageAccount.CreateCloudBlobClient();
_cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);
var path = downloadPath + file + "." + fileExtension;
using (var fileStream = System.IO.File.OpenWrite(path))
     {
          fileStream.Position = 1;
          blockBlob.DownloadToStreamAsync(fileStream);
          Console.WriteLine("success");
      }

结果如下:

enter image description here