Nếu bạn đang tìm kiếm một ví dụ điển hình về mã xử lý nhiều tệp, see here. Sau đó, bạn có thể view the source code.
Đây là nguồn gốc chính của bản demo (sửa đổi chỉ một chút):
var obj = this;
var model = (function() {
var zipFileEntry, zipWriter, writer, creationMethod, URL = obj.webkitURL || obj.mozURL || obj.URL;
return {
setCreationMethod : function(method) {
creationMethod = method;
},
addFiles : function addFiles(files, oninit, onadd, onprogress, onend) {
var addIndex = 0;
function nextFile() {
var file = files[addIndex];
onadd(file);
// Modified here to use the Data64URIReader instead of BlobReader
zipWriter.add(file.name, new zip.Data64URIReader(file.data), function() {
addIndex++;
if (addIndex < files.length)
nextFile();
else
onend();
}, onprogress);
}
function createZipWriter() {
zip.createWriter(writer, function(writer) {
zipWriter = writer;
oninit();
nextFile();
}, onerror);
}
if (zipWriter)
nextFile();
else if (creationMethod == "Blob") {
writer = new zip.BlobWriter();
createZipWriter();
} else {
createTempFile(function(fileEntry) {
zipFileEntry = fileEntry;
writer = new zip.FileWriter(zipFileEntry);
createZipWriter();
});
}
},
getBlobURL : function(callback) {
zipWriter.close(function(blob) {
var blobURL = creationMethod == "Blob" ? URL.createObjectURL(blob) : zipFileEntry.toURL();
callback(blobURL);
zipWriter = null;
});
},
getBlob : function(callback) {
zipWriter.close(callback);
}
};
})();
Cách sử dụng: Giả một yếu tố <a id="downloadLink">Download</a>
tồn tại để cung cấp việc tải xuống một lần đã sẵn sàng.
// Prepare your images
var files = [];
for (i = 0; i < len; i++) {
// Get the image URL from a SQLite request
var url = results.rows.item(i).url;
(function(url){
var img = new Image();
img.onload = function() {
// Add to file array [{name, data}]
var a = document.createElement('a');
a.href = this.src;
var filename= a.pathname.split('/').pop();
console.log("Loaded file " + filename);
files.push({name: filename, data: getBase64Image(img) });
}
img.src = url;
})(url);
}
// Wait for the image to load
var check = setInterval(function(){
if(files.length==images.length) {
clearInterval(check);
// Set the mode
model.setCreationMethod("Blob");
// Add the files to the zip
model.addFiles(files,
function() {
// Initialise Method
console.log("Initialise");
}, function(file) {
// OnAdd
console.log("Added file");
}, function(current, total) {
// OnProgress
console.log("%s %s", current, total);
}, function() {
// OnEnd
// The zip is ready prepare download link
// <a id="downloadLink" href="blob:url">Download Zip</a>
model.getBlobURL(function(url) {
document.getElementById("downloadLink").href = url;
document.getElementById("downloadLink").style.display = "block";
document.getElementById("downloadLink").download = "filename.zip";
});
});
}
}, 500);
Bạn có thể sử dụng mã nguồn ví dụ để thêm các chỉ báo tiến trình. Hy vọng điều này sẽ giúp, những điều tốt đẹp về phương pháp này là mô hình zip là dễ dàng tái sử dụng nếu bạn làm cho nó là tập tin JS của riêng.
Một suy nghĩ: Tôi đoán bạn đang sử dụng getBase64Image
chức năng from here, nếu như vậy và bạn vẫn gặp vấn đề tham nhũng, có lẽ cố gắng sửa đổi lại chỉ đơn giản là return dataURL;
và xóa bỏ .replace(...
, như Data64URIReader
có thể mong đợi các tiền tố .
Thx! :) Điều này dường như làm việc. Tôi có một vấn đề cuối cùng, chỉ có hình ảnh cuối cùng được lưu nhiều lần trong zip. Tôi nghĩ nó đến từ chức năng mô hình. Tôi đang nhìn vào nó. –
Tôi đã thực hiện thay đổi đối với mã một vài phút trước đây mà bạn có thể chưa nhận được. Trong trình tải hình ảnh đảm bảo có 'var' infront của URL hoặc hàm' onload' sẽ không nhận biến đúng cách. – Scott
Hãy cho tôi biết nếu bạn cần thêm trợ giúp. – Scott