如何从 Javascript Chrome iOS 下载 blob 文件?
How to download a blob file from Chrome iOS in Javascript?
如何从 Javascript Chrome iOS 下载 blob 文件?
我正在从 iOS 下载文件(pdf、excel、txt、png)。 iOS 没有文件系统,这对下载来说是个问题。
我创建了一个代码,该代码根据 OS 和导航器(如果需要)下载 blob 文件。它在桌面(Chrome 和 IE 的最新版本)、移动设备 Android(Chrome、本地导航器)和 iOS iPad2(Safari)上运行良好。
现在,Chrome iOS 应该像 Safari mobile 一样,但算法不起作用,Chrome iOs 通过在新文件中打开来下载文件选项卡,但页面是空的。
I create my own blob to create the downloadUrl.
This is a part of the download function .
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);
}
更多详情
Debugging on iPad 2 and iPhone 4
Trying to download an excell, pdf, txt, and Png files.
The devices doesn't have file system.
感谢您帮助我...如果您需要更多信息,请告诉我这是我的第一个问题。
经过搜索,这个解决方案似乎在 ChromeiOS 和 Safari 上运行良好。我在这个 post How to open Blob URL on Chrome iOS 上找到了它。这解决了我的问题:
//isbContentType i.e. 'text/plain'
//isbFilename i.e. 'This is a title'
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);
我相信有一种方法可以下载 blob 文件,但事实并非如此。还是谢谢
我们有一个 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');
}
var reader = new FileReader();
var out = new Blob([response.data], { type: 'application/pdf' });
reader.onload = function(e) {
window.location.href = reader.result;
}
reader.readAsDataURL(out);
// var blob = new Blob([response.data], { type: "application/pdf" });
var fileURL = URL.createObjectURL(out);
var a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = 'lkn_' + id + '.pdf';
document.body.appendChild(a);
a.click();
如何从 Javascript Chrome iOS 下载 blob 文件?
我正在从 iOS 下载文件(pdf、excel、txt、png)。 iOS 没有文件系统,这对下载来说是个问题。 我创建了一个代码,该代码根据 OS 和导航器(如果需要)下载 blob 文件。它在桌面(Chrome 和 IE 的最新版本)、移动设备 Android(Chrome、本地导航器)和 iOS iPad2(Safari)上运行良好。
现在,Chrome iOS 应该像 Safari mobile 一样,但算法不起作用,Chrome iOs 通过在新文件中打开来下载文件选项卡,但页面是空的。
I create my own blob to create the downloadUrl.
This is a part of the download function .
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);
}
更多详情
Debugging on iPad 2 and iPhone 4
Trying to download an excell, pdf, txt, and Png files.
The devices doesn't have file system.
感谢您帮助我...如果您需要更多信息,请告诉我这是我的第一个问题。
经过搜索,这个解决方案似乎在 ChromeiOS 和 Safari 上运行良好。我在这个 post How to open Blob URL on Chrome iOS 上找到了它。这解决了我的问题:
//isbContentType i.e. 'text/plain'
//isbFilename i.e. 'This is a title'
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);
我相信有一种方法可以下载 blob 文件,但事实并非如此。还是谢谢
我们有一个 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');
}
var reader = new FileReader();
var out = new Blob([response.data], { type: 'application/pdf' });
reader.onload = function(e) {
window.location.href = reader.result;
}
reader.readAsDataURL(out);
// var blob = new Blob([response.data], { type: "application/pdf" });
var fileURL = URL.createObjectURL(out);
var a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = 'lkn_' + id + '.pdf';
document.body.appendChild(a);
a.click();