Jquery: How to dynamically SHOW image using the “value” of <input type=“file”> Field

后端 未结 2 1987
天命终不由人
天命终不由人 2021-01-04 10:58

I was wondering whether there is a way to dynamically display an image that a user just uploaded to the input type=\"file\" field.

For example, so far I have the fol

2条回答
  •  粉色の甜心
    2021-01-04 11:32

    Take a look at this sample, this should work: http://jsfiddle.net/LvsYc/

    HTML:

    your image

    JS:

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

提交回复
热议问题