下面的代码可以工作。但它只下载了一张图片。我想要它下载几个图片,如果不是太麻烦,把它们放在一个文件夹,可能是一个压缩文件夹。由于某种原因,它不会下载超过一张照片(它说网络在其他图片上下载失败)。我试着实现JSZip,但是我想不出如何使用新的JSZip对象从服务器启动下载。有人能给我建议一些代码或者指出正确的方向吗?
最好让它发生在服务器上。
关于客户:
'click #download-all-photos': function (event, template) {
Files.find({
productId: this._id
}).forEach((photo) => {
Meteor.call('get.s3.image', photo, function (error, result) {
if (error) {
console.log(error);
} else {
function _arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i] );
}
return window.btoa(binary);
}
let base64 = _arrayBufferToBase64(result.data);
base64 = 'data:' +result.mimeType+ ';base64,'+base64;
let link = document.createElement('a');
link.setAttribute('href', base64);
link.setAttribute('download', result.fileName);
link.click();
link.remove();
}
});
});
}
});
服务器:
'get.s3.image': function (photo) {
let fileType = '';
let split = photo.url.split('.');
fileType = split[split.length-1];
let bucket = split[0].replace('https://', '');
var s3 = new AWS.S3({
accessKeyId: AWSAccessKeyId,
secretAccessKey: AWSSecretAccessKey
});
let split2 = photo.url.split('/');
let split3 = photo.url.split(split2[2] + '/');
let fileName = '';
fileName = split2[split2.length-1];
let key = split3[1];
let mimeType = mime.lookup(photo.url);
let fut = new Future();
s3.getObject(
{ Bucket: bucket, Key: key },
function (error, data) {
if (error) {
console.log("Failed to retrieve an object: " + error);
fut['return'](error);
throw new Meteor.Error('500', error);
} else {
console.log("Loaded " + data.ContentLength + " bytes");
fut['return']({
mimeType: mimeType,
fileName: fileName,
data: data.Body
});
}
}
);
return fut.wait();
},
我尝试过的另一件事是创建一条下载图像的路径。它同样有效,但我只能让它做一个图像。我宁愿它循环遍历被查询的图像集合,并将它们全部下载到zip文件夹中:
Router.route('/download_file/:_id', {
name: 'download.file',
where: 'server',
action: function () {
let res = this.response;
let image = Files.findOne(this.params._id);
if (!image) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end('404: No Images Found');
return;
}
let mimeType = mime.lookup(image.url);
let fileType = '';
let split = image.url.split('.');
fileType = split[split.length-1];
let bucket = split[0].replace('https://', '');
var s3 = new AWS.S3({
accessKeyId: AWSAccessKeyId,
secretAccessKey: AWSSecretAccessKey
});
let split2 = image.url.split('/');
let split3 = image.url.split(split2[2] + '/');
let key = split3[1];
s3.getObject(
{ Bucket: bucket, Key: key },
function (error, data) {
if (error != null) {
console.log("Failed to retrieve an object: " + error);
} else {
let headers = {
'Content-Type': mimeType,
'Content-Disposition': "attachment; filename=file."+fileType
};
res.writeHead(200, headers);
res.end(data.Body, 'binary');
}
}
);
}
});
发布于 2018-08-30 15:39:17
我好像解决了我自己的问题!也许有点烦躁。我让JSZip工作了。它从S3获取文件,然后将它们发送到客户端,然后下载到zip中(通过saveAs()
从流星包下载)。似乎效果很好。也许这能帮到别人。
客户端:
'click #download-all-photos': function (event, template) {
let JSZip = require('jszip');
let zip = new JSZip();
let folder = zip.folder('photos');
let hasError = false;
let fileName = '';
if (this.mpn) fileName += this.mpn + '_';
fileName += this.productNumber;
let count = 0;
let number = Files.find({ productId: this._id }).count();
let downloaded = false;
Files.find({ productId: this._id }).forEach((photo) => {
Meteor.call('get.s3.image', photo, function (error, result) {
if (error) {
console.log(error);
} else {
folder.file(result.fileName, result.data);
count++;
if (count === number && !downloaded) {
downloaded = true;
let zipFile = zip.generate();
let base64 = 'data:application/zip;base64,'+zipFile;
fetch(base64)
.then(res => res.blob())
.then(blob => {
saveAs(blob, fileName + '.zip');
});
}
}
});
});
}
服务器:
'get.s3.image': function (photo) {
let fileType = '';
let split = photo.url.split('.');
fileType = split[split.length-1];
let bucket = split[0].replace('https://', '');
var s3 = new AWS.S3({
accessKeyId: AWSAccessKeyId,
secretAccessKey: AWSSecretAccessKey
});
let split2 = photo.url.split('/');
let split3 = photo.url.split(split2[2] + '/');
let fileName = '';
fileName = split2[split2.length-1];
let key = split3[1];
let mimeType = mime.lookup(photo.url);
let fut = new Future();
s3.getObject(
{ Bucket: bucket, Key: key },
function (error, data) {
if (error) {
console.log("Failed to retrieve an object: " + error);
fut['return'](error);
throw new Meteor.Error('500', error);
} else {
console.log("Loaded " + data.ContentLength + " bytes");
fut['return']({
mimeType: mimeType,
fileName: fileName,
data: data.Body
});
}
}
);
return fut.wait();
},
https://stackoverflow.com/questions/52086090
复制相似问题