简体   繁体   English

如何在HTML中为移动页面创建图像下载链接?

[英]How can I create an Image Download Link in HTML for a Mobile Page?

I have a mobile html page which contains images. 我有一个包含图像的移动html页面。 I want to create a button or a link which is used for a download of an image. 我想创建一个用于下载图像的按钮或链接。 The image should then be saved to the users mobile image gallery. 然后应将图像保存到用户移动图像库。

I have seen this post: How can I create download link in html? 我看过这篇文章: 如何在html中创建下载链接?

The solution 解决方案

<a href="link/to/your/download/file" download="filename">Download link</a>

is working in desktop browsers but not on mobile. 正在桌面浏览器中工作,但不在移动设备上。

Here is a JSFiddle I made: http://jsfiddle.net/tDVqH/4/ 这是我制作的JSFiddle: http//jsfiddle.net/tDVqH/4/

Note: The image is created in the browser ie, in a HTML5 canvas element. 注意:图像是在浏览器中创建的,即在HTML5 canvas元素中创建。 This image can be generated with canvas.toDataUrl(). 可以使用canvas.toDataUrl()生成此图像。 The resulting image should be saved to the mobile image gallery. 生成的图像应保存到移动图像库中。

How can save an image to the users mobile image gallery with a click/tap? 如何通过点击/点击将图像保存到用户移动图库? Is there a JavaScript solution without the ser round trip with a unknown header? 是否有一个JavaScript解决方案没有带有未知头的ser往返?

Edit: I also found the following questions but they do not have an answer. 编辑:我也发现了以下问题,但他们没有答案。 Save an image to the local folder from the website by clicking a link or button in mobile browser and Save an image to a mobile phone gallery from a browser 通过单击移动浏览器中的链接或按钮从浏览器将图像保存到手机库将图像 从网站保存到本地文件夹

Somebody seems to have answered this already , 有人似乎已经回答了这个

 <a href="/path/to/image" download="ImageName" title="ImageName"> <img src="/path/to/image" alt="ImageName"> </a> It's not yet fully supported http://caniuse.com/#feat=download, but you can use with modernizr 

http://modernizr.com/download/#-a_download (under Non-core detects) to support all browsers. http://modernizr.com/download/#-a_download (在非核心检测下)支持所有浏览器。

Have not tested, but should work in mobiles as well. 没有经过测试,但也应该在移动设备上工作。

I would add that, as a server side solution, you could also add Response Headers to your download endpoint by 我想补充一点,作为服务器端解决方案,您还可以将响应标头添加到下载端点

  • Setting it up in apache (.htaccess) / nginx configuration 在apache(.htaccess)/ nginx配置中进行设置
  • Right from the code 从代码开始

Use html5 download attribute as solution above by @theunexpected1. 使用html5下载属性作为@theunexpected1的上述解决方案。 For browsers that don't support it, remove A tag to force user to right click or long touch to download 对于不支持它的浏览器,删除标签以强制用户右键单击或长按以下载

<script>
var downloadAttrSupported = ("download" in document.createElement("a"))
if (!downloadAttrSupported){
   $('img.media').unwrap();
}
</script>

Support for download attribute: http://caniuse.com/download 支持下载属性: http//caniuse.com/download

You can set headers on the server, if that's an option for you. 您可以在服务器上设置标头,如果这是您的选项。 The HTTP header you want to send is Content-Disposition: attachment; 您要发送的HTTP标头是Content-Disposition:attachment; filename="imagename.jpg". 文件名= “imagename.jpg”。

Differs depending on your server.. 根据您的服务器而有所不同..

In Node/Express: 在Node / Express中:

// only on imgs
function(req, res){
    res.setHeader('Content-disposition', 'attachment; filename=imagename.jpg');
}

In the HTML: 在HTML中:

<a href="imagename.jpg">Download link</a>

Server will send Content-Disposition header when it gets the request for the file and force browsers to download. 服务器在获取文件请求时会发送Content-Disposition标头并强制浏览器下载。

The force-download for files usually opened by the browser can be done with the .htaccess file in the directory where your images are. 通常由浏览器打开的文件强制下载可以使用图像所在目录中的.htaccess文件来完成。

It was replied previously here: 它先前在这里回复:

Force a file or image to download using .htaccess 使用.htaccess强制下载文件或图像

I haven't tested so don't know if it will work with mobile browsers though. 我没有测试过,所以不知道它是否适用于移动浏览器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM