我正在处理图像多上传,这听起来不错,但是..一如既往的内存问题。
脚本目标是在上传 100 多张图片 (300Mb+) 时存活下来。因此,如果您发现(我仍然是 javascript 蹩脚)任何问题,请给我一个建议。谢谢。
我的代码:
CFileReader.prototype.proccessFile = function(cb) {
// this means File
var reader = new FileReader();
reader.readAsDataURL(this);
reader.onload = (function (f) {
return function(e) {
var image = new Image();
image.src = e.target.result;
image.onload = (function(f) {
return function() {
var maxWidth = 700,
maxHeight = 700,
imageWidth = this.width,
imageHeight = this.height;
if (imageWidth > imageHeight) {
if (imageWidth > maxWidth) {
imageHeight *= maxWidth / imageWidth;
imageWidth = maxWidth;
}
}
else {
if (imageHeight > maxHeight) {
imageWidth *= maxHeight / imageHeight;
imageHeight = maxHeight;
}
}
var canvas = document.createElement('canvas');
canvas.width = imageWidth;
canvas.height = imageHeight;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0, imageWidth, imageHeight);
if(typeof cb == 'function') {
cb(f,canvas.toDataURL());
}
delete canvas;
delete ctx;
return;
}
})(f);
};
})(this);
}