function getKmzFileAsText() {
var text = '';
var xhr = new XMLHttpRequest(),
fileReader = new FileReader();
xhr.open("GET", "kmz/1.kmz", true);
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
// Load blob as Data URL
fileReader.readAsDataURL(xhr.response);
JSZip.loadAsync(xhr.response).then(function (content) {
// if you return a promise in a "then", you will chain the two promises
return content.files["doc.kml"].async('text');
})
.then(function (txt) {
alert(txt);
text = String(txt);
});
}
}, false);
// Send XHR
xhr.send();
console.log(text);
// return text;
}
function getKmzFileAsText(callback) {
var text = '';
var xhr = new XMLHttpRequest(),
fileReader = new FileReader();
xhr.open("GET", "kmz/1.kmz", true);
// Set the responseType to blob
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
// Load blob as Data URL
fileReader.readAsDataURL(xhr.response);
JSZip.loadAsync(xhr.response)
.then(function (content) {
// if you return a promise in a "then", you will chain the two promises
return content.files["doc.kml"].async('text');
})
.then(function (txt) {
alert(txt);
text = String(txt);
return callback(text);
});
}
}, false);
// Send XHR
xhr.send();
console.log(text);
}
getKmzFileAsText(function(result) {
var text = result
});
function getKmzFileAsText() {
return new Promise(function(resolve, reject){
var text = '';
var xhr = new XMLHttpRequest(),
fileReader = new FileReader();
xhr.open("GET", "kmz/1.kmz", true);
xhr.responseType = "blob";
xhr.addEventListener("load", function () {
if (xhr.status === 200) {
// Load blob as Data URL
fileReader.readAsDataURL(xhr.response);
JSZip.loadAsync(xhr.response)
.then(function (content) {
// if you return a promise in a "then", you will chain the two promises
resolve(content.files["doc.kml"].async('text'));
}).then(resolve, reject);
}else{
reject(Error(xhr.status))
}
}, false);
xhr.ontimeout= xhr.onerror= reject;
xhr.send();
});
}
getKmzFileAsText().then(function(result){
console.log(result);
});