Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

azure - I want to download a file from storage

file path

https://ubj10edustgcdn.azureedge.net/userdata/documents/a09b372d-cffe-438a-b98d-123d118ac0a5/provincial management service batch.pdf

how can i downlaod file from this path by c#

var filepath ="https://ubj10edustgcdn.azureedge.net/userdata/documents/a09b372d-cffe-438a-b98d-123d118ac0a5/provincial management service batch.pdf";
return  File(filepath, "application/force-download", Path.GetFileName(filepath));

this code return exception that virtual path is not valid.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The URL contains azureedge.net, which means it's served from a CDN. Which also means you will not be downloading from storage, but 'just' a file from the internet.

Have a look at How to download a file from a URL in C#?

using (var client = new WebClient())
{
   client.DownloadFile("http://example.com/file/song/a.mpeg", "a.mpeg");
}

If you actually want to download a file from Azure Storage, you will need to know the (internal) path to the file and you can use code like this to download the file:

    string connectionString = CloudConfigurationManager.GetSetting("StorageConnection");
    BlobContainerClient container = new BlobContainerClient(connectionString, "<CONTAINER-NAME>");

    var blockBlob = container.GetBlobClient("<FILE-NAME>");
    using (var fileStream = System.IO.File.OpenWrite("<LOCAL-FILE-NAME>"))
    {
      blockBlob.DownloadTo(fileStream);
    }

Source: Uploading and Downloading a Stream into an Azure Storage Blob


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...