如何强制用户下载图像(例如下载pdf)? [英] how do I force users to download image (like downloading pdf)?

查看:94
本文介绍了如何强制用户下载图像(例如下载pdf)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我编写了一个图片库,其中包含下载原始图像的选项(默认情况下,它将显示该图像的调整大小的版本).我很想知道如何

So I wrote an image gallery with an option to download the original image (by default, it displays a resized version of the image). I would love to know how to I

以下代码将强制用户保存pdf而不是使用浏览器查看pdf,我希望通过将click动作与jQuery绑定来实现图像(jpg,gif,png)的相同效果,我将如何要这样做吗?

The following code will force users to save pdf rather than viewing it with browser, I would love to achieve the same effect for the images (jpg, gif, png) by binding the click action with jQuery, how will I be able to do that?

来自php.net

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

html:

<div class="download">
<a href="http://www.xyz/abc.jpg">
    Download original image</a>
</div>

js:

$(".download").click(function(){
  // call a php to download the image
});

是否可以这样做?

我应该使用javascript代码做什么?

What should I do in javascript code?

推荐答案

有可能.服务器(PHP脚本)需要发送Content-Disposition标头,如第一个代码片段所示.例如:

It's possible. The server (PHP script) needs to send the Content-Disposition header as show in your first snippet. For example:

header('Content-Disposition: attachment; filename="abc.jpg"');

这篇关于如何强制用户下载图像(例如下载pdf)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆