强制浏览器在点击下载图像文件

编程入门 行业动态 更新时间:2024-10-27 02:27:01
本文介绍了强制浏览器在点击下载图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有一种方法可以使用客户端编程来实现这一点?

我需要浏览器下载图像文件,就像它在点击Excel表单时一样。只有?

< html xmlns =www.w3/1999/xhtml> < head> < title>< / title> < script type =text / javascriptsrc =Scripts / jquery-1.10.2.js> $(document).ready(function(){ $(*)。click(function(){ $(p)。hide(); }); }); < / script> < / head> < script type =text / javascript> document.onclick = function(e){ e = e || window.event; var element = e.target || e.srcElement; if(element.innerHTML ==Image){ //someFunction(element.href); var name = element.nameProp; var address = element.href; saveImageAs1(element.nameProp,element.href); 返回false; //防止默认动作并停止事件传播} else return true; }; $ b $ function saveImageAs1(name,adress){ if(confirm('you want to save this image?')){ window.win = open(adress); //response.redirect(\"~/testpage.html); setTimeout('win.document.execCommand(SaveAs)',100); setTimeout('win.close()',500); } } < / script> < body> < form id =form1runat =server> < div> < p> < a href =http:// localhost:55298 / SaveImage / demo / Sample2.xlsxtarget =_ blank> Excel< / a>< br /> < a href =http:// localhost:55298 / SaveImage / demo / abc.jpgid =abc>图片< / a> < / p> < / div> < / form> < / body> < / html>

如何下​​载Excel表格(浏览器)?

解决方案

使用HTML5,您可以将属性'download'添加到您的链接中。 < a href =/ path / to / image.png下载>

下载具有相同文件名的图像(在这个例子中为image.png)。

如果你为这个属性指定一个值,那么它将成为新的文件名:

< a href =/ path / to / image.pngdownload =AwesomeImage.png> 更新:截至2018年春天,对于跨源 href 取值即可。因此,如果您想在imgur以外的域名上创建< a href =i.imgur/IskAzqA.jpg下载> 将无法按预期工作。 Chrome的弃用和移除公告

p>

I need the browser to download the image files just as it does while clicking on an Excel sheet.

Is there a way to do this using client-side programming only?

<html xmlns="www.w3/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="Scripts/jquery-1.10.2.js"> $(document).ready(function () { $("*").click(function () { $("p").hide(); }); }); </script> </head> <script type="text/javascript"> document.onclick = function (e) { e = e || window.event; var element = e.target || e.srcElement; if (element.innerHTML == "Image") { //someFunction(element.href); var name = element.nameProp; var address = element.href; saveImageAs1(element.nameProp, element.href); return false; // Prevent default action and stop event propagation } else return true; }; function saveImageAs1(name, adress) { if (confirm('you wanna save this image?')) { window.win = open(adress); //response.redirect("~/testpage.html"); setTimeout('win.document.execCommand("SaveAs")', 100); setTimeout('win.close()', 500); } } </script> <body> <form id="form1" runat="server"> <div> <p> <a href="localhost:55298/SaveImage/demo/Sample2.xlsx" target="_blank">Excel</a><br /> <a href="localhost:55298/SaveImage/demo/abc.jpg" id="abc">Image</a> </p> </div> </form> </body> </html>

How should it work in case of downloading an Excel sheet (what browsers do)?

解决方案

Using HTML5 you can add the attribute 'download' to your links.

<a href="/path/to/image.png" download>

Compliant browsers will then prompt to download the image with the same file name (in this example image.png).

If you specify a value for this attribute, then that will become the new filename:

<a href="/path/to/image.png" download="AwesomeImage.png">

UPDATE: As of spring 2018 this is no longer possible for cross-origin hrefs. So if you want to create <a href="i.imgur/IskAzqA.jpg" download> on a domain other than imgur it will not work as intended. Chrome deprecations and removals announcement

更多推荐

强制浏览器在点击下载图像文件

本文发布于:2023-11-01 20:41:06,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:点击下载   图像文件   浏览器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!