我的代码:
<div>
<a href="/images/test-201.gif" class="download">Download</a>
</div>
我需要做的是当我点击下载时。它应该打开一个新窗口,例如保存为该图像。我需要使用 HTML 或 javascript 来执行此操作。
简而言之:您不能仅使用 javascript 和 html 来实现这一点。
如果没有服务器端语言(如 php),您将无法强制下载文件。服务器需要将图像连同正确的响应头一起发送给客户端
简短的回答:这是不可能的。您必须将其发布到后端(服务器)并使用 header 从服务器响应它Content-disposition: attachment
。
这也不能作为AJAX
回应,所以你必须做类似的事情
document.location = 'download_image.php?file=test-201.gif';
如上所述,它使用正确的标头响应下载。
据我所知,这是唯一通过 javascript 触发下载的跨浏览器解决方案。
您不能仅使用 javascript 来实现这一点。
如果没有服务器端语言(如 php),您将无法做到这一点。服务器需要将图像发送给客户端。
You can do this using an attribute known as download. Just add the attribute download="test image" to your anchor tag. "test image" can be any name you want to give to the image. Here is the syntax :
<a href="/images/test-201.gif" class="download" download="test image">Download</a>