【问题标题】:How to preview selected image in input type="file" in popup using jQuery? [duplicate]如何使用jQuery在弹出窗口中预览输入类型=“文件”中的选定图像? [复制]
【发布时间】:2013-08-29 17:20:42
【问题描述】:

在我的代码中,我允许用户上传图片。现在我想在同一个弹出窗口中将这个选定的图像显示为预览。我如何使用 jQuery 来做到这一点?

以下是我在弹出窗口中使用的输入类型。

HTML 代码:

<input type="file" name="uploadNewImage">

【问题讨论】:

标签: jquery html


【解决方案1】:

Demo

HTML:

 <form id="form1" runat="server">
   <input type='file' id="imgInp" />
   <img id="blah" src="#" alt="your image" />
</form>

jQuery

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$("#imgInp").change(function(){
    readURL(this);
});

Reference

【讨论】:

  • 感谢 prabu,但它没有给出图像的完整路径,它只是返回一个图像名称
  • 您使用的浏览器
  • 除 IE 外的所有 chrome、mozilla、opera、safari
  • 使用 $('#imgInp').val() 获取完整路径
  • 在 IE9 或 IE8 中不支持,但除此之外它有很好的支持...caniuse.com/#feat=filereader
【解决方案2】:

如果您使用的是 HTML5,请尝试以下代码 sn-p

<img id="uploadPreview" style="width: 100px; height: 100px;" />
<input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
<script type="text/javascript">

    function PreviewImage() {
        var oFReader = new FileReader();
        oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);

        oFReader.onload = function (oFREvent) {
            document.getElementById("uploadPreview").src = oFREvent.target.result;
        };
    };

</script>

【讨论】:

  • 接受的答案有什么不同?我没有看到任何与 HTML5 相关的差异......(顺便说一句,onload 声明应该在 .readAsDataURL 方法调用之前)
【解决方案3】:

您可以使用URL.createObjectURL

    function img_pathUrl(input){
        $('#img_url')[0].src = (window.URL ? URL : webkitURL).createObjectURL(input.files[0]);
    }
#img_url {
  background: #ddd;
  width:100px;
  height: 90px;
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="" id="img_url" alt="your image">
<br>
<input type="file" id="img_file" onChange="img_pathUrl(this);">

【讨论】:

    【解决方案4】:

    只需检查我的脚本是否运行良好:

      function handleFileSelect(evt) {
        var files = evt.target.files; // FileList object
    
        // Loop through the FileList and render image files as thumbnails.
        for (var i = 0, f; f = files[i]; i++) {
    
          // Only process image files.
          if (!f.type.match('image.*')) {
            continue;
          }
    
          var reader = new FileReader();
    
          // Closure to capture the file information.
          reader.onload = (function(theFile) {
            return function(e) {
              // Render thumbnail.
              var span = document.createElement('span');
              span.innerHTML = ['<img class="thumb" src="', e.target.result,
                                '" title="', escape(theFile.name), '"/>'].join('');
              document.getElementById('list').insertBefore(span, null);
            };
          })(f);
    
          // Read in the image file as a data URL.
          reader.readAsDataURL(f);
        }
      }
    
      document.getElementById('files').addEventListener('change', handleFileSelect, false);
    
    #list img{
      width: auto;
      height: 100px;
      margin: 10px ;
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用 ajax 上传来预览您选择的文件.. http://zurb.com/playground/ajax-upload

      【讨论】:

      • 但是如果他想在上传文件之前预览文件怎么办?
      猜你喜欢
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多