表单提交前预览图像 [英] preview image before form submit

查看:41
本文介绍了表单提交前预览图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有 ImageField.有没有办法在提交表单之前显示所选文件?也许在 jQuery 中是可能的?

I have ImageField in my form. Is there any way to show chosen file before submitting a form? Maybe it is possible in jQuery?

我读到我可以通过 request.FILES 以某种方式访问​​这些文件,但我认为在我提交表单之前它会是空的.

I read that I can somehow access this files with request.FILES, but I think it will be empty before I will submit form.

推荐答案

试试这个.

function upload_img(input) {
  if (input.files && input.files[0]) {
    var src = URL.createObjectURL(input.files[0])
    $('#img_id').attr('src', src)
  }
}

和 HTML 在下面.

AND HTML is below.

谢谢.

<form id="form_img">
  <input type='file' onchange="upload_img(this);" />
  <img id="img_id" src="#" alt="your image" />
</form>

这可能对您有所帮助.

这篇关于表单提交前预览图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆