简体   繁体   English

在 Firefox 中从 blob 下载图像

[英]Downloading image from blob in Firefox

I've tried implementing image download from blob by following the answer from this post (the one from @Jonathan Amend).我已经尝试按照这篇文章的答案(来自@Jonathan Amend 的答案)从 blob 实现图像下载。 The download function works perfectly in Google Chrome.下载功能在谷歌浏览器中完美运行。 However, upon testing it on Firefox, the file I end up getting is blob instead of jpg/png/etc.但是,在 Firefox 上对其进行测试时,我最终得到的文件是 blob 而不是 jpg/png/etc。 Is there any additional code necessary to make it work properly in Firefox?是否需要任何额外的代码才能使其在 Firefox 中正常工作? My code looks exactly the same in the ajax with the exception using atob on my data before passing it as new blob like so:我的代码在 ajax 中看起来完全相同,但在将数据作为新 blob 传递之前对我的数据使用 atob 的异常,如下所示:

 var blob = new Blob([atob(data)], { type: type });

Not sure what's going on but for some strange reason, even if the type is set, the extension of the file does not get set in firefox.不确定发生了什么,但出于某种奇怪的原因,即使设置了类型,文件的扩展名也不会在 Firefox 中设置。 To fix this, I checked if browser is firefox and added the file type extension in the filename like so:为了解决这个问题,我检查了浏览器是否是 firefox 并在文件名中添加了文件类型扩展名,如下所示:

if(navigator.userAgent.search("Firefox") >- 1) {
     var temp = type.split('/');
     filename = filename + "." + temp[1];
}

Perhaps you suggest a better solution?也许您提出了更好的解决方案?

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

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