Javascript,以编程方式触发Firefox中的文件下载 [英] Javascript, programmatically trigger file download in firefox
问题描述
这个小提琴的工作原理是chrome而不是FF:http://jsfiddle.net/6W2TY/
当您点击运行它将下载小图像铬并且在FF中什么也不做。任何人都可以帮助我理解为什么它在FF中不起作用,需要做些什么来使其发挥作用?
谢谢!
您正在使用新的(html5?)下载属性。据我所知,这仅仅是在Chrome中支持,而Firefox中并不支持。
使用location.href
另一种强制下载的方法是将用户重定向到图像:
//生成图像
var img =
//然后调用函数也许onClick或某物
downloadImage(img);
函数downloadImage(data){
location.href =data:application / octet-stream; base64,+ data;
}
或简称
location.href = 数据:应用/八位字节流;的base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAOCAYAAAAmL5yKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAABWSURBVDhPY0xISPh // 0UOA7mAiVyNMH2jBjAwkBQGjD9KGBTEJ6OEO0kG2NvbMwCjnXwDsEU5SS5ANuDhjRCGJbPFSQsDdBfIyMhQZgDIQLK9QLWkDABPsQw5I + 5qmAAAAABJRU5ErkJggg ==
服务器端
另外,如果您正在处理图像服务器端,您可以通过设置强制下载内容处理标题。
PHP示例
header('Content-Disposition:attachment; filename =image.png);
I have a data uri in memory that I would like the user to download.
This fiddle works in chrome but not FF: http://jsfiddle.net/6W2TY/
When you click run it will download the tiny image in chrome and do nothing in FF. Can anyone help me understand why it doesn't work in FF and what I need to do to make it work?
Thanks!
You are using the new (html5?) download attribute. As far as I know this is only supported in Chrome and not (yet) in Firefox.
Using location.href
Another way to force a download is to redirect the user to the image like this:
// generate the image
var img = ""
// then call a function maybe onClick or something
downloadImage(img);
function downloadImage(data) {
location.href = "data:application/octet-stream;base64," + data;
}
Or the short version
location.href = "data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAOCAYAAAAmL5yKAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAABWSURBVDhPY0xISPh//0UOA7mAiVyNMH2jBjAwkBQGjD9KGBTEJ6OEO0kG2NvbMwCjnXwDsEU5SS5ANuDhjRCGJbPFSQsDdBfIyMhQZgDIQLK9QLWkDABPsQw5I+5qmAAAAABJRU5ErkJggg=="
Server Side
As an alternative, if you are processing the image serverside you can force a download by setting the content-disposition header.
PHP Example
header('Content-Disposition: attachment; filename="image.png"');
这篇关于Javascript,以编程方式触发Firefox中的文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!