简体   繁体   English

如何从MVC中的URL下载图像

[英]How to download an image from URL in MVC

In my Asp.net MVC I had used following code when the image file kept in an actual folder structure. 在我的Asp.net MVC中,当图像文件保存在实际的文件夹结构中时,我使用了以下代码。

public FileResult DownloadFile(string imageName)
        {
            string fullPath = this.GetFullPath(imageName);
            string contentType = " image/pjpeg";

            return new FilePathResult(fullPath, contentType)
            {
                FileDownloadName = imageName
            };
        }

However now we have moved images to Azure Blob, And how can we download the image from there. 但是,现在我们已将图像移至Azure Blob,以及如何从那里下载图像。 Since FilePathResult dint work out we tried using the following code. 由于FilePathResult dint可以解决,因此我们尝试使用以下代码。

public ActionResult DownloadFile(string imgName)
        {            
            string fullPath = imgName;            
            CloudStorageAccount account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConString"));
            CloudBlobClient client = account.CreateCloudBlobClient();
            CloudBlobContainer blobContainer = client.GetContainerReference(ConfigurationManager.AppSettings["BlobReference"]);

            CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(imgName);

            return Redirect(blockBlob.Uri.AbsoluteUri);
        }

However the files are not downloading, instead it opens in a new window, Please guide me to download the file instead of opening in the new window. 但是文件未下载,而是在新窗口中打开。请指导我下载文件,而不是在新窗口中打开。

So there are two ways by which you can accomplish this thing: 因此,有两种方法可以完成此任务:

1. Assuming you would always want the file to be downloaded 1.假设您始终希望下载文件

If you want the file to be always downloaded, you can set the Content-Disposition property of the blob as attachment; filename=yourdesiredfilename 如果要始终下载文件,可以将Blob的Content-Disposition属性设置为attachment; filename=yourdesiredfilename attachment; filename=yourdesiredfilename . attachment; filename=yourdesiredfilename In this case blockBlob.Uri.AbsoluteUri will always force the file to download. 在这种情况下, blockBlob.Uri.AbsoluteUri将始终强制文件下载。

2. Assuming you want the flexibility of both downloading the file sometimes and opening the file in the browser 2.假设您希望既灵活有时下载文件,又在浏览器中打开文件

In this case, you could create a Shared Access Signature (SAS) on the blob and specify Content-Disposition header as a part of SAS. 在这种情况下,您可以在Blob上创建一个共享访问签名(SAS),并指定Content-Disposition标头作为SAS的一部分。 Here's the sample code to do so: 这是执行此操作的示例代码:

        CloudStorageAccount acc = new CloudStorageAccount(new StorageCredentials("<accountname>", "<accountKey>"), true);
        var client = acc.CreateCloudBlobClient();
        var container = client.GetContainerReference("<containername>");
        var blob = container.GetBlockBlobReference("<imagename.png>");
        var sasToken = blob.GetSharedAccessSignature(new SharedAccessBlobPolicy()
        {
            SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
            Permissions = SharedAccessBlobPermissions.Read
        }, new SharedAccessBlobHeaders()
        {
            ContentDisposition = "attachment;filename=<imagename.png>",
        });
        var blobUrl = blob.Uri.AbsoluteUri + sasToken;
        Redirect(blobUrl);

My recommendation would be to go with #2 as it gives you more security + you will be able to share blobs from private blob containers as well. 我的建议是选择#2,因为它可以为您提供更高的安全性,并且您还可以共享私有Blob容器中的Blob。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM