如何在 Javascript 中从 Chrome iOS 下载 blob 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29134418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download a blob file from Chrome iOS in Javascript?
提问by Es Noguera
How to download a blob file from Chrome iOS in Javascript ?
如何在 Javascript 中从 Chrome iOS 下载 blob 文件?
I'm working on download files (pdf, excel, txt, png) from iOS. iOS hasn't a file systems which is a problem for downloads. I create a code which download a blob file depending of the OS and the navigator if is required. It works nicely on desktop (lastest versions of Chrome and IE), Mobile Android (Chrome, native navigator) and iOS iPad2 (Safari).
我正在从 iOS 下载文件(pdf、excel、txt、png)。iOS 没有文件系统,这对下载来说是个问题。如果需要,我创建了一个代码,根据操作系统和导航器下载 blob 文件。它在桌面(最新版本的 Chrome 和 IE)、移动 Android(Chrome、本机导航器)和 iOS iPad2 (Safari) 上运行良好。
Now, Chrome iOS supposed to be like Safari mobile, but the algorithm doesn't works, Chrome iOs download the file by opening in a new tab but the page is empty.
现在,Chrome iOS 应该像 Safari mobile 一样,但是算法不起作用,Chrome iOs 通过在新选项卡中打开来下载文件,但页面是空的。
I create my own blob to create the downloadUrl.
This is a part of the download function .
我创建了自己的 blob 来创建 downloadUrl。
这是下载功能的一部分。
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(iobBLOB);
var newWindow = null;
if (typeof a.download === 'undefined') {
var newWindow = null;
newWindow = window.open(downloadUrl, '_blank');
setTimeout(function() {
newWindow.document.title = isbFilename;
}, 10);
}
More details
更多细节
Debugging on iPad 2 and iPhone 4
Trying to download an excell, pdf, txt, and Png files.
The devices doesn't have file system.
在 iPad 2 和 iPhone 4 上调试
尝试下载 Excel、pdf、txt 和 Png 文件。
这些设备没有文件系统。
Thanks for helping me... Tell me if you need more information it's my first question.
感谢您帮助我...如果您需要更多信息,请告诉我这是我的第一个问题。
采纳答案by Es Noguera
After searching, this solution seems to works fine on ChromeiOS and Safari. I found it on this post "How to open Blob URL on Chrome iOS". This solved my problem:
搜索后,此解决方案似乎在 ChromeiOS 和 Safari 上运行良好。我在这篇文章“如何在 Chrome iOS 上打开 Blob URL”中找到了它。这解决了我的问题:
var reader = new FileReader();
reader.onload = function(e) {
var bdata = btoa(reader.result);
var datauri = 'data:' + isbContentType + ';base64,' + bdata;
window.open(datauri);
newWindow = setTimeout(function() {
newWindow.document.title = isbFilename;
}, 10);
};
reader.readAsBinaryString(iobBLOB);
I believed there was a way to download the blob file, but it wasn't. Thanks anyway
我相信有一种方法可以下载 blob 文件,但事实并非如此。不管怎么说,还是要谢谢你
回答by Bob Walker
We have a wordpress site, and on the callback to download a csv file we had to add the following for ipad/pod/phone specific download;
我们有一个 wordpress 站点,在下载 csv 文件的回调中,我们必须为 ipad/pod/phone 特定下载添加以下内容;
if(stristr($_SERVER['HTTP_USER_AGENT'], 'ipad') OR stristr($_SERVER['HTTP_USER_AGENT'], 'iphone') OR stristr($_SERVER['HTTP_USER_AGENT'], 'ipod')) {
header("Content-Type: application/octet-stream");
} else {
header('Content-Type: application/vnd.ms-excel');
}