Javascript,以编程方式触发Firefox中的文件下载 [英] Javascript, programmatically trigger file download in firefox

查看:175
本文介绍了Javascript,以编程方式触发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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆