<html>
<body>
<a href=".png" download> download</a>
</body>
</html>
<html>
<body>
<a href="https://www.google..ua/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" download> download</a>
</body>
</html>
Hello! Any suggestions to force save-as dialogue for image w/o .htaccess?
Share Improve this question edited Mar 23, 2017 at 4:15 Унгамист Долматов asked Mar 23, 2017 at 4:03 Унгамист ДолматовУнгамист Долматов 1131 silver badge8 bronze badges 8- Hey, you misspelled "as" in your title. Might want to fix that, someone will probably flag your question if you don't. – Anish Goyal Commented Mar 23, 2017 at 4:05
- 3 @AnishGoyal ty, i'm a bit sleepy – Унгамист Долматов Commented Mar 23, 2017 at 4:09
- @JaromandaX it's looks like too heavy solution – Унгамист Долматов Commented Mar 23, 2017 at 4:14
- Possible duplicate of force browser to download image files on click – jarodsmk Commented Mar 23, 2017 at 4:22
- 1 This is a browser (chrome ? ) feature. Your user may configure it as he wishes. i.sstatic/BvOD8.png And even FileSaver.js can't do anything about it... – Kaiido Commented Mar 23, 2017 at 4:42
4 Answers
Reset to default 4This is a browser (chrome ? ) feature.
Your users may configure it as they wish.
Neither you nor even FileSaver.js can do anything about it...
Adapted from the post: force browser to download image files on click
Since the html5 'download' attribute will still only work for pliant browsers. (As per @RichardParnaby-King's answer)
You can try:
var link = document.createElement('a');
link.href = 'images.jpg';
link.download = 'Download.jpg';
document.body.appendChild(link);
link.click();
(Thanks to @DrowsySaturn's answer) It's also worth mentioning that since it's JS, some older browsers won't support this.
Since browsers each have their own way of handling links, generally browsers will aim to display an image if specified by a URL on an tag and not automatically download them. This is aimed to be rectified with the download attribute in HTML5 but obviously some browsers wouldn't have yet implemented (and some may never).
PS: Try search for your question first to prevent duplicates!
The most reliable approach is to force it on the server side.
For your convenience, the browser will automatically handle particular content in specific ways. In particular, the browser will automatically handle an image by displaying it inline, which is what you are trying to avoid.
How does the browser know that it’s an image, as opposed to some other content. The server sends a header to say so. In the case of png image, the server sends something like this:
Content-Type: image/png
.
The trick is to get the server to also send a preferred method of handling the content. In your case you need a header like this:
Content-disposition: attachment; filename=…
This will tell the browser to download it.
A simple PHP script to do this would be something like this:
// assuming png
$filename=@_GET['filename'];
$data=file_get_contents($filename);
header("Content-disposition: attachment; filename=$filename");
header('Content-type: image/png');
print $data;
Solved by @Kaiido's ment:
This is a browser (chrome ? ) feature. Your user may configure it as he wishes. https://i.sstatic/BvOD8.png And even FileSaver.js can't do anything about it...
Most chrome-based browsers (Opera in my case) has this option enabled by default.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745333045a4622972.html
评论列表(0条)