从输入字段抓取图像以获取图像数据/显示图像
我一直在四处寻找,但无法找到这是否可能。
这就是我希望在提交之前发生的情况:当用户选择要上传的文件时,我想抓取图像的数据并将其转换为 base64。转换后,我想直接将其显示在 div
中,或者通过 AJAX 将其发送到服务器,然后显示在 div
中。
以下基本上是我正在寻找的内容:
// index.php
<input type="file" name="img" id="img" onChange="displayImg(this)">
// displayImg.js
function displayImg(img) {
imgData = img.?; // How do I do this?
img64 = // I know how to do this.
document.write("<img src='data:image/jpeg;base64,"+img64+"' />");
}
I've been looking around and haven't been able to find whether this is possible or not.
This is what I would like to happen before submitted: When a user selects a file to be uploaded, I want to grab the image's data and convert it to base64. After it's been converted, I would like to either directly display it in a div
or have it sent to the server via AJAX, then displayed in the div
.
Below is basically what I'm looking for:
// index.php
<input type="file" name="img" id="img" onChange="displayImg(this)">
// displayImg.js
function displayImg(img) {
imgData = img.?; // How do I do this?
img64 = // I know how to do this.
document.write("<img src='data:image/jpeg;base64,"+img64+"' />");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码对我有用。它缺乏验证和所有这些好东西,但这对您来说应该是一个很好的起点。
The code above works for me. It lacks verification and all that good stuff, but this should be a good starting point for you.