0

When I press download and call this action, I get the result

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

and directed to

http://integratedproject20170322032906.azurewebsites.net/MyDocumentUps/Download/2020Resume.pdf

Why does it link to above and not to

https://filestorageideagen.blob.core.windows.net/documentuploader/2020Resume.pdf

as shown in the controller?

Here is my actionlink in the view

@Html.ActionLink("Download", "Download", "MyDocumentUps", new { id = item.DocumentId.ToString() + item.RevisionId.ToString() + item.Attachment }, new { target="_blank"}) |

public ActionResult Download(string id)
        {

            string path = @"https://filestorageideagen.blob.core.windows.net/documentuploader/";

            return View(path + id);

        }
Milen
  • 8,697
  • 7
  • 43
  • 57

2 Answers2

2

I can think of 2 ways by which you can force download a file in the client browser.

  1. Return aFileResultorFileStreamResultfrom your controller. Here's an example of doing so: How can I present a file for download from an MVC controller?. Please note that this will download the file first on your server and then stream the contents to the client browser from there. For smaller files/low load this approach may work but as your site grows or the files to be downloaded becomes bigger in size, it will create more stress on your web server.
  2. Use a Shared Access Signature (SAS) for the blob with Content-Disposition response header set. In this approach you will simply create a SAS token for the blob to be downloaded and using that create a SAS URL. Your controller will simply return a RedirectResult with this URL. The advantage with this approach is that all the downloads are happening directly from Azure Storage and not through your server.

When creating SAS, please ensure that

  • You have at least Read permission in the SAS.
  • Content-Disposition header is overridden in the SAS.
  • The expiry of SAS should be sufficient for the file to download.

Here's the sample code to create a Shared Access Signature.

        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            Permissions = SharedAccessBlobPermissions.Read,
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),

        }, new SharedAccessBlobHeaders()
        {
            ContentDisposition = "attachment;filename=" + blob.Name
        });
        var blobUrl = string.Format("{0}{1}", blob.Uri.AbsoluteUri, sasToken);
        return Redirect(blobUrl);

P.S. While I was answering the question, the question got edited so the answer may seem a bit out of whack :) :).

Community
  • 1
  • 1
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
1

Given that you have the image url as part of your Model and blob has no restrictions:

<img src=@Model.YourImageUri>

var image = new BitmapImage(new Uri("https://your_storage_account_name.blob.core.windows.net/your_container/your_image.jpg"));

Milen
  • 8,697
  • 7
  • 43
  • 57