我正在使用JQuery blob将JQuery数组导出为CSV。 它适用于除Safari 5.1.7之外的所有浏览器。 Windows 7上的Safari浏览器。 我来了解了Blob与Safari的兼容性问题。 如果有任何解决方法,请告诉我。
以下是代码:
var usersCSVData = [];
usersCSVData.push('LastName ','FirstName ', 'Login ','City ','State','Location ');
var fileName = "UserCSVdata.csv";
var buffer = usersCSVData.join("\n");
var blob = new Blob([buffer], {
"type": "text/csv;charset=utf8;"
});
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, fileName);
}
else {
var link = document.createElement("a");
if (link.download !== undefined) {
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", fileName);
link.style = "visibility:hidden";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
我收到错误:
"'[object BlobConstructor]' is not a constructor (evaluating 'new Blob([buffer], {
"type": "text/csv;charset=utf8;"
})')"
答案 0 :(得分:0)
首先尝试使用BlobBuilder
或WebKitBlobBuilder
。使用blob构造函数/构建器也解决了Android Stock Browser
4.4 - :
//cross browser BlobBuilder constructor
var customBlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MSBlobBuilder || window.MozBlobBuilder;
//result
var blob;
//Data
var buffer = ['LastName ','FirstName ', 'Login ','City ','State','Location '].join("\n");
//Try to use WebKitBlobBuilder first, It also solves some issues for Android Stock Browser
if (window.WebKitBlobBuilder) {
blob = new WebKitBlobBuilder();
blob.append(buffer);
blob = blob.getBlob("text/csv");
} else if (window.Blob) {
blob = new Blob([buffer], { type : "text/csv" });
} else {
blob = new customBlobBuilder();
blob.append(buffer);
blob = blob.getBlob("text/csv");
}
console.log(blob);