-1

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.

Tayyeb
  • 127
  • 7
  • How about returning a 302 redirect with that URL? Or you need to start a download through a stream and return that stream in File(). – juunas Jan 11 '21 at 09:55

1 Answers1

0

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

rickvdbosch
  • 14,105
  • 2
  • 40
  • 53
  • UnauthorizedAccessException: Access to the path 'c:\windows\system32\inetsrv\a.mpeg' is denied. I got excepton like that – Tayyeb Jan 11 '21 at 12:42
  • System.NotSupportedException: 'The given path's format is not supported.' If i pass this Url "https://ubj10edustgcdn.azureedge.net/userdata/documents/a09b372d-cffe-438a-b98d-123d118ac0a5/provincial management service batch.pdf" – Tayyeb Jan 11 '21 at 12:45