可靠的 Javascript 图像预览

发布于 2024-08-30 22:55:03 字数 255 浏览 5 评论 0 原文

我有以下代码,向用户展示他们尝试上传的图像的预览,并且在 FF 中运行得非常好:

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

问题是,getAsDataURL() 仅在 FF 中运行。 Chrome 中是否有类似的功能/解决方法(特别是)?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

邮友 2024-09-06 22:55:03

如果浏览器不支持 getAsDataURL,您可以确保文件输入使用 Gears 的 openFiles(向下滚动)读取用户选择的文件。
Google 不再在 Gears 上工作,但它可以在 Chrome 上工作,至少在 getAsDataURL() 实现之前是这样。

编辑:更改了答案以使其更有帮助。

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.

淡墨 2024-09-06 22:55:03

IE 尚不支持文件 API。无论如何,您需要使用 FileReader 来读取文件。另外,该文件不是它的文件名(您的变量命名有点含糊)。

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

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文