【问题标题】:how to upload multiple images and preview all uploaded如何上传多张图片并预览所有上传的图片
【发布时间】:2020-04-29 16:24:21
【问题描述】:

此代码适用于单个图像,但我需要执行多个,因此请帮助我更改此代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
                <div class="col">
                    <div><img id="img1" src="" alt="your image" style="max-width:180px;" /><output id=”result” ></div>
                    <div><input id="img11" onchange="readURL(this);" type="file"  name="Image1" id="" style="padding:10px; background:#2d2d2d;" ></div>
                </div>

                <div class="col">
                    <div><img id="img2" src="" alt="your image" style="max-width:180px;" /><output id=”result” ></div>
                    <div><input id="img22" onchange="readURL(this);" type="file"  name="Image1" id="" style="padding:10px; background:#2d2d2d;" ></div>
                </div>


            </div

【问题讨论】:

  • 为什么?你做不到的原因是什么?

标签: javascript jquery laravel bootstrap-4


【解决方案1】:

如果你想选择多个图像文件并在 dom 上显示它!然后你可以做这样的事情: 我想这就是你要找的东西

HTML:

<input type="file" multiple id="gallery-photo-add">
<div class="gallery"></div>

JS:

<script>

$(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
                }

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

    };

    $('#gallery-photo-add').on('change', function() {
        imagesPreview(this, 'div.gallery');
    });
});

</script>

【讨论】:

  • 它可以工作,但输入值仅获取第一张图像数据,如何解决该问题
  • 哪个输入值?请描述。你在 input.files 中有一个选定文件的数组
猜你喜欢
  • 1970-01-01
  • 2014-09-27
  • 2014-01-13
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多