【问题标题】:downloading a picture or showing save popup for a picture on click on a button [duplicate]单击按钮下载图片或显示图片的保存弹出窗口[重复]
【发布时间】:2013-09-11 12:34:08
【问题描述】:

我在这里进退两难。好吧,我想要一个按钮来下载图片,而不是在浏览器上显示或至少要求保存图片。

我试图在 href 中找到图像 url,但它是在单击图片显示在 rbowser 上的链接时找到的。知道我在做什么错吗? 这是链接代码

 <a href="images/gallery/1.jpg">  Download This Image</a>  

单击此下载图像会在新浏览器窗口中显示 1.jpg。 !!

谢谢。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    主要有两种方法可以做到这一点:

    客户端

    <a href="images/gallery/1.jpg" download="images/gallery/1.jpg">Download This Image</a>
    

    注意:这个won't work cross-browser

    服务器端

    发送Content-Disposition 标头以强制客户端下载文件。您可能希望使用可选参数来允许直接显示图像。 所以你会选择类似的东西

    if($_GET['download']){
        header('Content-Disposition: attachment; filename="filename.ext"');
    }
    

    假设您使用的是 PHP

    【讨论】:

      【解决方案2】:

      您可以使用download 属性,但它是not supported by all browsers

      <a href="images/gallery/1.jpg" download="/path/to/image">  Download This Image</a>  
      

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 2022-01-10
        • 2015-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多