How to preview an uploaded image as the background image of a div?

后端 未结 1 829
梦谈多话
梦谈多话 2021-01-13 00:46

I want to preview an uploaded image file in a div. I have done a bit of research and I have found this piece of code from this post, it is the code to preview an uploaded im

相关标签:
1条回答
  • You could use CSS shorthand just like in a CSS file. I recommend the following to avoid repeating and alignment issues:

    <script type="text/javascript">
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
    
                reader.onload = function (e) {
                    $('#objectID').css('background', 'transparent url('+e.target.result +') left top no-repeat');
                }
    
                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>
    

    The change is the line like this

    $('#objectID').css('background', 'transparent url('+e.target.result +') left top no-repeat');
    
    0 讨论(0)
提交回复
热议问题