Jason
Jason

Reputation: 52533

Unfailing Javascript Image Preview

I have the following code that presents the user with a preview of the image they're trying to upload and works really well in FF:

var img = document.createElement('img');
img.src = $('#imageUploader').get(0).files[0].getAsDataURL();

The problem is, getAsDataURL() only works in FF. Is there something similar/a workaround for this kind of functionality in Chrome (specifically)?

Upvotes: 9

Views: 3255

Answers (2)

Aram Mkrtchyan
Aram Mkrtchyan

Reputation: 2700

IE does not yet support the File API. Anyhow, you need to use a FileReader to read a file. Also, the file is not its file name (your variable naming is a little ambiguous).

file = fileObj.files[0];
var fr = new FileReader;
fr.onloadend = changeimg;
fr.readAsDataURL(file)

Upvotes: 0

Henrik Hansen
Henrik Hansen

Reputation: 2190

If the browser doesn't support getAsDataURL you could make sure that the file input is instead using Gears' openFiles (scroll down) to read a file selected by the user. Google ain't working on Gears anymore, but it will work in Chrome, at least until getAsDataURL() gets implemented.

EDIT: Changed answer to be more helpful.

Upvotes: 2

Related Questions