最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Javascript FileReader eats up all the memory when reads local images - Stack Overflow

programmeradmin1浏览0评论

I'm working on a concent to show the thumbnail of a large number of images from the local drive.

With HTML5 File API is seems quite possible, but whn I try to load a big number of images the memory usage of the browser goes over the roof and the collapses.

I think the problem is that the FileReader doesn't releases the memory after a file read.

Originally I had a new instance of FileReader and a simple loop to iterate through the images.

To solve this memory problem I replaced this to have one FileReader only, but it did not really help.

Here is the relevand code block:

<script>

var areader = new FileReader();
var counter=0;

function loadImage(file) {
    var canvas = document.createElement("canvas");
    areader.onload = function (event) {
        var img = new Image;

        img.onload = function () {
            canvas.width = img.width / 100;
            canvas.height = img.height / 100;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width / 100, img.height / 100);
            var browse = document.getElementById("uploadInput");
            if (browse.files.length > counter) {
                counter++;
                areader.result = null;//I don't think this makes any difference
                loadImage(browse.files[counter]);
            }
        };

        img.src = event.target.result;
    };
    areader.readAsDataURL(file);
    preview.appendChild(canvas);
}

function showImages() {
    loadImage(document.getElementById("uploadInput").files[0]);

}

If anybody e across this problem the I do something very stupid could you reply.

Thanks,

Tamas

I'm working on a concent to show the thumbnail of a large number of images from the local drive.

With HTML5 File API is seems quite possible, but whn I try to load a big number of images the memory usage of the browser goes over the roof and the collapses.

I think the problem is that the FileReader doesn't releases the memory after a file read.

Originally I had a new instance of FileReader and a simple loop to iterate through the images.

To solve this memory problem I replaced this to have one FileReader only, but it did not really help.

Here is the relevand code block:

<script>

var areader = new FileReader();
var counter=0;

function loadImage(file) {
    var canvas = document.createElement("canvas");
    areader.onload = function (event) {
        var img = new Image;

        img.onload = function () {
            canvas.width = img.width / 100;
            canvas.height = img.height / 100;
            var ctx = canvas.getContext("2d");
            ctx.drawImage(img, 0, 0, img.width / 100, img.height / 100);
            var browse = document.getElementById("uploadInput");
            if (browse.files.length > counter) {
                counter++;
                areader.result = null;//I don't think this makes any difference
                loadImage(browse.files[counter]);
            }
        };

        img.src = event.target.result;
    };
    areader.readAsDataURL(file);
    preview.appendChild(canvas);
}

function showImages() {
    loadImage(document.getElementById("uploadInput").files[0]);

}

If anybody e across this problem the I do something very stupid could you reply.

Thanks,

Tamas

Share asked Nov 18, 2012 at 17:28 snecisneci 311 silver badge3 bronze badges 2
  • How many images are we talking about here? – Pointy Commented Nov 18, 2012 at 17:37
  • And how big are the images, too? Is this a problem in all browsers or just one? – Sean Redmond Commented Nov 18, 2012 at 17:50
Add a ment  | 

1 Answer 1

Reset to default 10

It's not the file reader but you are using the entire image's data in base64 as the src property of the image, which will actually take 133% of the image's size in memory.

You should use Blob URLs instead:

var URL = window.URL || window.webkitURL;

function loadImage( file ) {
    var canvas = document.createElement("canvas"),
        img = new Image();

    img.onload = function() {
        canvas.width = img.width / 100;
        canvas.height = img.height / 100;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width / 100, img.height / 100);
        URL.revokeObjectURL( img.src );
        img = null;

        var browse = document.getElementById("uploadInput");
            if (browse.files.length > counter) {
            counter++;
            loadImage(browse.files[counter]);
        }
    };
    img.src = URL.createObjectURL( file );

    preview.appendChild(canvas);
}
发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>