html - 直接从链接下载图片

标签 html css asp.net-mvc-3 href

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

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

我的页面有点像 tumblr 博客,主页上有几张图片,每张图片旁边都有“下载 HQ”按钮。

最佳答案

添加 HTTP header :

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

要在“另存为”对话框中显示的文件名在哪里(如 finances.xls 或 mortgage.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);
}

关于html - 直接从链接下载图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12230663/

相关文章:

asp.net - 在基本 Controller 中重写 ASP.NET MVC 中的 OnAuthorization

c# - MVC3 应用程序中的 BackgroundWorker 卡住 UI

javascript - Canvas 'drawImage' 返回 ' No function was found that matched the signature provided.'

html - 页面在 html 和 css 中没有一直滚动到底部

css - 具有生成样式数据绑定(bind)的引用 Assets

c# - 有没有办法防止 Html.ListBoxFor 剪切所选元素的溢出文本?

c# - 如何使用转换从 web.config 中删除元素?

html - 宽度为零的表格列

jquery - 如何更改 JtaCarousel Jquery 中的幻灯片时间(自动播放)?

css - 如何使背景图像适合包含的 anchor 元素?