Unfailing Javascript Image Preview
Asked Answered
V

2

9

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)?

Vindicable answered 30/4, 2010 at 22:30 Comment(7)
pretty much. You will need to use gears, flash, or java. In IE6 you can just get the value of the file upload and use "file:///..." to display the preview, But I think they 86'd that in ie7.Adin
i don't believe you. someone must know of a non-flash way.Vindicable
Isn't the .get() method supposed to load data from the server with a GET request? (api.jquery.com/jQuery.get) Maybe you meant .eq()?Wellnigh
There is not such a way. The standard process is stalled, when it is confirmed, the Firefox team will update their API (which they are now discouraging the public to use, check developer.mozilla.org/en/DOM/File) to conform to it, and the Webkit one will implement itConlan
I simply refuse to believe that the only way to achieve this effect is with flash or an applet. SOMEONE has to know of a way. Especially with all the web8 effects out thereVindicable
you are stubborn and kinda grumpy.Adin
it's these damn kids always on my lawnVindicable
H
2

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.

Heterocercal answered 2/5, 2010 at 17:39 Comment(0)
I
0

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)
Idolism answered 9/11, 2011 at 14:56 Comment(1)
But, eh, the question isn't about IE, it's about Chrome. (The question is also really old - that code should work in the current version of Chrome).Pubescent

© 2022 - 2024 — McMap. All rights reserved.