简体   繁体   English

无法使用的Javascript图像预览

[英]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: 我有以下代码向用户显示他们尝试上传的图像的预览,并在FF中工作得非常好:

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

The problem is, getAsDataURL() only works in FF. 问题是, getAsDataURL()仅适用于FF。 Is there something similar/a workaround for this kind of functionality in Chrome (specifically)? 在Chrome中是否有类似的/这种功能的解决方法(特别是)?

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. 如果浏览器不支持getAsDataURL您可以确保文件输入使用Gears的openFiles (向下滚动)来读取用户选择的文件。 Google ain't working on Gears anymore, but it will work in Chrome, at least until getAsDataURL() gets implemented. 谷歌不再使用Gears,但它可以在Chrome中运行,至少在getAsDataURL()实现之前。

EDIT: Changed answer to be more helpful. 编辑:更改答案更有帮助。

IE does not yet support the File API. IE尚不支持File API。 Anyhow, you need to use a FileReader to read a file. 无论如何,您需要使用FileReader来读取文件。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM