Подтвердить что ты не робот

Как просмотреть выбранное изображение в input type="file" во всплывающем меню с помощью jQuery?

В моем коде я разрешаю пользователю загружать изображение. Теперь я хочу показать это выбранное изображение как предварительный просмотр в этом же всплывающем окне. Как я могу сделать это с помощью jQuery?

Ниже приведен тип ввода, который я использую во всплывающем окне.

Код HTML:

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

Ответ 1

Демо

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);
});

Ссылка

Ответ 2

Если вы используете HTML5, попробуйте выполнить фрагмент кода

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

Ответ 3

Вы можете использовать загрузку ajax для предварительного просмотра выбранного файла. http://zurb.com/playground/ajax-upload

Ответ 4

<script>
function img_pathUrl(input){
   $('#img_url')[0].src = (window.URL ? URL : webkitURL).createObjectURL(input.files[0]);
}
</script>

<img src="" id="img_url" alt="your image">
<iput type="file" id="img_file" onChange="img_pathUrl(this);">

Ответ 5

Просто проверьте, что мои скрипты работают хорошо:

  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 ;
}