我生成正常链接,例如: >
一个网络应用程序。
当我点击链接时,它会在新页面中显示图片。如果你想保存图片,那么你需要右键单击它并选择“另存为”
我不想要这种行为,我希望当我点击链接时弹出一个下载框,可以吗只是用 html 或 javascript 吗?如何?
如果不是,我想我必须编写一个 download.php 脚本并将其调用到 href 中,并以文件名作为参数......?
I generate normal links like: <a href="/path/to/image"><img src="/path/to/image" /></a>
in a web app.
When I click on the link, it displays the picture in a new page. If you want to save the picture, then you need to right click on it and select "save as"
I don't want this behaviour, I would like to have a download box popping out when I click on the link, is that possible just with html or javascript? How?
If not I guess I would have to write a download.php script and call it into the href with the file name as parameter...?
发布评论
评论(10)
尚未完全支持 caniuse,但您可以与 modernizr(在非核心检测下)检查浏览器的支持情况。
It's not yet fully supported caniuse, but you can use with modernizr (under Non-core detects) to check the support of the browser.
创建图像或 html 下载链接的最简单方法是设置下载属性,但此解决方案仅适用于现代浏览器。
“myimage”是要下载的文件的名称。扩展名将自动添加
此处示例
The easiest way of creating download link for image or html is setting download attribute, but this solution works in modern browsers only.
"myimage" is a name of file to download. Extension will be added automatically
Example here
下载.php:
download.php:
如果您使用 HTML5,您可以将“下载”属性添加到您的链接中。
http://www.w3schools.com/tags/att_a_download.asp
If you are Using HTML5 you can add the attribute 'download' to your links.
http://www.w3schools.com/tags/att_a_download.asp
不,不是。您将需要服务器上的某些内容来发送 Content-Disposition 标头,以将文件设置为附件而不是内联。不过,您可以使用普通的 Apache 配置来完成此操作。
我发现 使用 mod_rewrite 执行此操作的示例,尽管我知道有一种更简单的方法。
No, it isn't. You will need something on the server to send a Content-Disposition header to set the file as an attachment instead of being inline. You could do this with plain Apache configuration though.
I've found an example of doing it using mod_rewrite, although I know there is a simpler way.
试试这个...
Try this...
HTML 下载属性指定当用户单击超链接时将下载目标。
仅当设置了 href 属性时才使用此属性。
该属性的值将是下载文件的名称。允许的值没有限制,浏览器会自动检测正确的文件扩展名并将其添加到文件中(.img、.pdf、.txt、.html 等)。
示例代码:
HTML5:
输出:
下载图片>>
Html5下载或chrome
下载图片>>
HTML download attribute to specify that the target will be downloaded when a user clicks on the hyperlink.
This attribute is only used if the href attribute is set.
The value of the attribute will be the name of the downloaded file. There are no restrictions on allowed values, and the browser will automatically detect the correct file extension and add it to the file (.img, .pdf, .txt, .html, etc.).
Example code:
HTML5:
Output:
Download Image >>
Html5 download or chrome
Download Image >>
你不能用纯 html/javascript 来做到这一点。这是因为您有一个与网络服务器的单独连接来检索单独的文件(图像),并且普通的网络服务器将提供设置了内容标头的文件,以便读取内容类型的浏览器将决定该类型可以在内部处理。
强制浏览器不在内部处理文件的方法是更改标头(最好是内容处置或内容类型),这样浏览器就不会尝试在内部处理文件。您可以通过在网络服务器上编写动态设置标头的脚本(即 download.php)或配置网络服务器为您要下载的文件返回不同的标头来实现此目的。您可以在网络服务器上的每个目录上执行此操作,这将允许您无需编写任何 php 或 javascript - 只需将所有下载图像放在该位置即可。
You can't do it with pure html/javascript. This is because you have a seperate connection to the webserver to retrieve a separate file (the image) and a normal webserver will serve the file with content headers set so that the browser reading the content type will decide that the type can be handled internally.
The way to force the browser not to handle the file internally is to change the headers (content-disposition prefereably, or content-type) so the browser will not try to handle the file internally. You can either do this by writing a script on the webserver that dynamically sets the headers (i.e. download.php) or by configuring the webserver to return different headers for the file you want to download. You can do this on a per-directory basis on the webserver, which would allow you to get away without writing any php or javascript - simply have all your download images in that one location.
使用 php 单击图像进行图像下载的简单代码
Simple Code for image download with an image clicking using php
使用图像点击下载图像!
我做了这个简单的代码!:)
Image download with using image clicking!
I did this simple code!:)