I'm using FileReader to upload image files to a client for data fetching and thumbnails display.
what I've noticed is, that on the page process, in task manager, the memory just keeps going higher and higher. and when the process stops, and memory stay high and never goes down.
can you please tell me what I am doing wrong here?
to check, please upload more then 200 pictures, up to 30MG. and see that the memory keeps on leaking
thank you in advanced.
-- here is a link to a code example on the web
and here is my code:
<input class="fu" type="file" multiple="multiple" />
<div class="fin"></div>
<div class="list"></div>
<script type="text/javascript">
$(document).ready(function () {
var input = $("body input.fu");
input[0].addEventListener('change', fu.select, false);
});
var fu = {
list: [],
index: 0,
select: function (evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.target.files ? evt.target.files : evt.dataTransfer ? evt.dataTransfer.files : []; // FileList object
fu.list = files;
fu.index = 0;
fu.load();
},
load: function () {
var index = fu.index;
var file = fu.list[index];
if (file) {
var reader = new FileReader(); // File API object
reader.onloadend = (function (theFile) {
return function (evt) {
if (evt.target.readyState == FileReader.DONE) {
setTimeout(fu.load, 20);
}
};
})(file);
reader.onprogress = null;
reader.onloadstart = null;
reader.onerror = null;
reader.onabort = null;
if (reader.readAsBinaryString) {
reader.readAsBinaryString(file);
} else {
reader.readAsDataURL(file);
}
fu.index++;
$('.fin').html("#" + fu.index);
} else {
$('.fin').html("finish");
}
}
}
</script>
ok, I have fixed this.
the reason was that I set reader to - new FileReader() - each time.
so I only made it global.
here is the working code:
<script type="text/javascript">
$(document).ready(function () {
var input = $("body input.fu");
input[0].addEventListener('change', fu.select, false);
});
var fu = {
list: [],
index: 0,
reader: null,
select: function (evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.target.files ? evt.target.files : evt.dataTransfer ? evt.dataTransfer.files : []; // FileList object
fu.list = files;
fu.index = 0;
fu.reader = new FileReader(); // <- made this only once
fu.load();
},
load: function () {
var index = fu.index;
var file = fu.list[index];
if (file) {
fu.reader.onloadend = (function (theFile) {
return function (evt) {
if (evt.target.readyState == FileReader.DONE) {
fu.reader.abort();
setTimeout(fu.load, 5);
}
};
})(file);
fu.reader.onprogress = null;
fu.reader.onloadstart = null;
fu.reader.onerror = null;
fu.reader.onabort = null;
if (fu.reader.readAsBinaryString) {
fu.reader.readAsBinaryString(file);
} else {
fu.reader.readAsDataURL(file);
}
fu.index++;
$('.fin').html("#" + fu.index);
} else {
$('.fin').html("finish");
}
}
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With