问题描述:

Hi I am making a mobile web application that requires a user to upload multiple images on to the server. Once they select their images, I want to show a thumbnail preview of their images before they choose to upload to the server. I have tried both FileReader way and the createObjectURL (using a canvas) and both methods take ages for the images to appear. This normally would not be an issue but most images on a mobile phone are high quality and are at least 2-3mb.

The time it takes for the thumbnails to generate is almost the same as actually uploading the image. Is there any way I can just show an instant low res preview of the images before uploading?

The following are two ways that I have used to try and preview images:

createObjectURL and Canvas way

<input type="file" id="browseImages" multiple="multiple" accept="image/*">

<output id="list"></output>

document.getElementById('browseImages').addEventListener('change', handleFileSelect, true);

function handleFileSelect(evt) {

var files = evt.target.files; // FileList object

// Loop through the FileList and render image files as thumbnails.

for (var i = 0, f; f = files[i]; i++) {

// Only process image files.

if (!f.type.match('image.*')) {

continue;

}

var canvas = document.createElement('canvas');

canvas.width = 110;

canvas.height = 100;

var ctx = canvas.getContext("2d");

var img = new Image;

img.onload = function() {

ctx.drawImage(img, 0, 0, 100, 100);

}

img.src = window.URL.createObjectURL(f);

document.getElementById('list').appendChild(canvas);

window.URL.revokeObjectURL(f);

}

}

The above way reads the images that the user selected and creates a canvas with the image for each image that was selected.

FileReader way

<input type="file" id="browseImages" multiple="multiple" accept="image/*">

<output id="list"></output>

document.getElementById('browseImages').addEventListener('change', handleFileSelect, true);

function handleFileSelect(evt) {

var files = evt.target.files; // FileList object

// Loop through the FileList and render image files as thumbnails.

for (var i = 0, f; f = files[i]; i++) {

// Only process image files.

if (!f.type.match('image.*')) {

continue;

}

var reader = new FileReader();

// Closure to capture the file information.

reader.onload = (function(theFile) {

return function(e) {

// Render thumbnail.

var span = document.createElement('span');

span.innerHTML = ['<img style="height:75px;" src="', e.target.result,

'" title="', escape(theFile.name), '"/>'].join('');

document.getElementById('list').insertBefore(span, null);

};

})(f);

// Read in the image file as a data URL.

reader.readAsDataURL(f);

}

}

The above way uses a FileReader to read and then process the preview images.

Both these above methods take time going through each image, reading it and then processing a thumbnail. The speed of it is as if I am uploading the images. Is there any way I can generate instant thumbnails of the selected images? Bare in mind this is for a mobile browser, so it will not have a big memory like a PC.

相关阅读:
Top