预览2个或更多个输入文件的图像

时间:2017-04-13 15:55:12

标签: javascript file input preview

我有2个图片字段,我想在提交表单之前显示预览。在下面的表格中,它通过显示多个图像来工作,但我希望每个输入分别显示其图像:

输入1 输入1张图片

输入2 输入2图像

我该怎么做?

<input id="fileupload" type="file" name="img_slide" multiple>
                  <div id="dvPreview">

<input id="fileupload2" type="file" name="img_capa" multiple>
              <div id="dvPreview2">

<script>
          window.onload = function () {

            var fileUpload = document.getElementById("fileupload");

        fileUpload.onchange = function () {
          if (typeof (FileReader) != "undefined") {
        var dvPreview = document.getElementById("dvPreview");
        dvPreview.innerHTML = "";
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
        for (var i = 0; i < fileUpload.files.length; i++) {
            var file = fileUpload.files[i];
            if (regex.test(file.name.toLowerCase())) {
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = document.createElement("IMG");

                    img.height = "100";
                    img.width = "100";
                    img.src = e.target.result;
                    dvPreview.appendChild(img);
                    dvPreview.appendChild(textbox);
                }
                reader.readAsDataURL(file);
            } else {
                alert(file.name + " is not a valid image file.");
                dvPreview.innerHTML = "";
                return false;
            }
        }
    } else {
        alert("This browser does not support HTML5 FileReader.");
    }
}

};               

1 个答案:

答案 0 :(得分:0)

我试过这个并且适合我。代码:

<pre>Please enter your files:<input class="fileupload" type="file" name="img_slide" multiple>
              <div id="dvPreview"></div></pre>

<input class="fileupload" type="file" name="img_capa" multiple>
          <div id="dvPreview2"></div>



window.onload = function () {

    var fileUpload = document.getElementsByClassName("fileupload");

    for(var i = 0; i < fileUpload.length; i++){
    fileUpload[i].onchange = showImgOnChange;
    }




}


var showImgOnChange = function () {

    if (typeof (FileReader) != "undefined") {
        var dvPreview = this.nextElementSibling;

        dvPreview.innerHTML = "";
        var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;

            for (var i = 0; i < this.files.length; i++) {
            var file = this.files[i];
            if (regex.test(file.name.toLowerCase())) {
            var reader = new FileReader();
            reader.onload = function (e) {
                var img = document.createElement("IMG");

                img.height = "100";
                img.width = "100";
                img.src = e.target.result;
                dvPreview.appendChild(img);
                dvPreview.appendChild(textbox);
            }
            reader.readAsDataURL(file);
            } else {
            alert(file.name + " is not a valid image file.");
            dvPreview.innerHTML = "";
            return false;
            }

                    }
        } else {
        alert("This browser does not support HTML5 FileReader.");
        }
}

现在我将解释该代码:

我为该类型的每个输入使用单个类。使用该类我得到所有元素输入,并在每个输入上分配onchange函数showImgOnChange(只是将更改中的相同函数关联到多个元素的技巧)。之后,在函数中,概括一下:

var dvPreview = document.getElementById("dvPreview");

我用过这个:

var dvPreview = this.nextElementSibling;

这将获取DOM中此元素的下一个元素。否则,您可以将一个类关联到div,它将显示图像并搜索具有该类的下一个元素。

希望有所帮助