0

我想知道是否有办法为图像制作直接下载链接?当我做这样的事情时:

它在浏览器或新选项卡中打开图像。我希望当用户单击该链接时立即下载图像。有没有办法用 HTML/CSS 或 ASP.NET MVC 3 做到这一点?

我的页面类似于 tumblr 博客,主页上有几张图片,每个旁边都有“下载总部”按钮。

4

2 回答 2

2

您将需要服务器端脚本以使链接可下载,您需要将标头设置为

Content-Disposition:attachment;filename=some.jpg

然后读取图像的内容并将其刷新到响应对象。

这是 asp.net 的示例

例子

于 2012-09-01T19:21:57.463 回答
2

添加 HTTP 标头:

Content-Disposition: attachment; filename=<file name.ext> 

您希望在“另存为”对话框中显示的文件名在哪里(如财务.xls 或抵押.pdf) - 不带 < 和 > 符号。

这是 MVC 框架中的一个示例:

public ActionResult Download()
{
    var document = ...
    var cd = new System.Net.Mime.ContentDisposition
    {
        // for example foo.bak
        FileName = document.FileName, 

        // always prompt the user for downloading, set to true if you want 
        // the browser to try to show the file inline
        Inline = false, 
    };
    Response.AppendHeader("Content-Disposition", cd.ToString());
    return File(document.Data, document.ContentType);
}
于 2012-09-01T19:24:14.020 回答