如何在jQuery和Laravel中使用一个输入字段上传多个图像和文件,并进行预览。

huangapple go评论87阅读模式
英文:

How to upload Multiple Images and Files in one input field with Preview in jquery and Laravel

问题

<div class="col-lg-12 mt-4">
    <label for="file">{{ __('上传收据/账单(多个文件)') }}</label>
    <div class="upload__box">
        <div class="upload__btn-box">
            <label class="upload__btn">
                {{ __('上传账单') }}
                <input type="file" multiple="" id="files" name="files[]" data-max_length="20" class="upload__inputfile" accept="image/jpeg, image/jpg, image/png, application/pdf">
            </label>
        </div>
        <div class="upload__img-wrap"></div>
    </div>
</div>

I'm trying to upload Multiple Images and Files with preview and cancel but unable to find any way to do so.

英文:
&lt;div class=&quot;col-lg-12 mt-4&quot;&gt;
                            &lt;label for=&quot;file&quot;&gt;{{ __(&#39;Upload Receipts/Bills (Multiple Document)&#39;) }}&lt;/label&gt;
                            &lt;div class=&quot;upload__box&quot;&gt;
                                &lt;div class=&quot;upload__btn-box&quot;&gt;
                                  &lt;label class=&quot;upload__btn&quot;&gt;
                                    {{__(&#39;Upload Bills&#39;)}}
                          
                                    &lt;input type=&quot;file&quot; multiple=&quot;&quot; id=&quot;files&quot; name=&quot;files[]&quot; data-max_length=&quot;20&quot; class=&quot;upload__inputfile&quot; accept=&quot;image/jpeg, image/jpg, image/png, application/pdf&quot;&gt;
                                  &lt;/label&gt;
                                &lt;/div&gt;
                                &lt;div class=&quot;upload__img-wrap&quot;&gt;&lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;

I'm trying to upload Multiple Images and Files with preview and cancel but unable to find any way to do so.

答案1

得分: 1

$(document).ready(function () {
  ImgUpload();

  function ImgUpload() {
    var imgWrap = "";
    var imgArray = [];

    $('.upload__inputfile').each(function () {
      $(this).on('change', function (e) {
        imgWrap = $(this).closest('.upload__box').find('.upload__img-wrap');
        var maxLength = $(this).attr('data-max_length');

        var files = e.target.files;
        var filesArr = Array.prototype.slice.call(files);
        var iterator = 0;

        filesArr.forEach(function (f, index) {

          // if (!f.type.match('image.*')) {
          // return;
          // }

          if (imgArray.length > maxLength) {
            return false
          } else {
            var len = 0;
            for (var i = 0; i < imgArray.length; i++) {
              if (imgArray[i] !== undefined) {
                len++;
              }
            }
            if (len > maxLength) {
              return false;
            } else {
              imgArray.push(f);

              var reader = new FileReader();
              reader.onload = function (e) {
                console.log(f.type);
                if (f.type == 'application/pdf') {
                  var html = "<div class='upload__img-box'><div style='background-image: url(" + e.target.result + ")' data-number='" + $(".upload__img-close").length + "' data-file='" + f.name + "' class='img-bg ' ><div class='upload__img-close'></div><img src='https://cdn-icons-png.flaticon.com/128/337/337946.png'></div></div>";

                } else {
                  var html = "<div class='upload__img-box'><div style='background-image: url(" + e.target.result + ")' data-number='" + $(".upload__img-close").length + "' data-file='" + f.name + "' class='img-bg ' ><div class='upload__img-close'></div></div></div>";

                }
                imgWrap.append(html);
                iterator++;
              }
              reader.readAsDataURL(f);
            }
          }
        });
      });
    });

    $('body').on('click', ".upload__img-close", function (e) {
      var file = $(this).parent().data("file");
      for (var i = 0; i < imgArray.length; i++) {
        if (imgArray[i].name === file) {
          imgArray.splice(i, 1);
          break;
        }
      }
      $(this).parent().parent().remove();
      $(this).parent().children().remove();

    });
  }
});
<style>
.upload__box {
  padding-top: 10px;
}

.upload__inputfile {
  width: 0.1px;
  height: 0.1px;
  opacity: 0;
  overflow: hidden;
  position: absolute;
  z-index: -1;
}

.upload__btn {
  display: inline-block;
  font-weight: 600;
  color: #fff;
  text-align: center;
  min-width: 116px;
  padding: 5px;
  transition: all 0.3s ease;
  cursor: pointer;
  border: 2px solid;
  background-color: #4045ba;
  border-color: #4045ba;
  border-radius: 10px;
  line-height: 26px;
  font-size: 14px;
}

.upload__btn:hover {
  background-color: unset;
  color: #4045ba;
  transition: all 0.3s ease;
}

.upload__btn-box {
  margin-bottom: 0px;
}

.upload__img-wrap {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -10px;
}

.upload__img-box {
  width: 200px;
  padding: 0 10px;
  margin-bottom: 0px;
}

.upload__img-close {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  position: absolute;
  top: 10px;
  right: 10px;
  text-align: center;
  line-height: 24px;
  z-index: 1;
  cursor: pointer;
}

.upload__img-close:after {
  content: '✖';
  font-size: 14px;
  color: white;
}

.img-bg {
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  position: relative;
  padding-bottom: 100%;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form action="">
  <div class="col-lg-12 mt-4">
    <label for="file">Upload Document (Multiple Document)</label>
    <div class="upload__box">
      <div class="upload__btn-box">
        <label class="upload__btn">
        Upload file
        <input type="file" multiple="" id="files" name="files[]" data-max_length="20" class="upload__inputfile" accept="image/jpeg, image/jpg, image/png, application/pdf">
        </label>
      </div>
      <div class="upload__img-wrap"></div>
    </div>
  </div>
</form>
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).ready(function () {
ImgUpload();
function ImgUpload() {
var imgWrap = &quot;&quot;;
var imgArray = [];
$(&#39;.upload__inputfile&#39;).each(function () {
$(this).on(&#39;change&#39;, function (e) {
imgWrap = $(this).closest(&#39;.upload__box&#39;).find(&#39;.upload__img-wrap&#39;);
var maxLength = $(this).attr(&#39;data-max_length&#39;);
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
var iterator = 0;
filesArr.forEach(function (f, index) {
// if (!f.type.match(&#39;image.*&#39;)) {
// return;
// }
if (imgArray.length &gt; maxLength) {
return false
} else {
var len = 0;
for (var i = 0; i &lt; imgArray.length; i++) {
if (imgArray[i] !== undefined) {
len++;
}
}
if (len &gt; maxLength) {
return false;
} else {
imgArray.push(f);
var reader = new FileReader();
reader.onload = function (e) {
console.log(f.type);
if (f.type == &#39;application/pdf&#39;) {
var html = &quot;&lt;div class=&#39;upload__img-box&#39;&gt;&lt;div style=&#39;background-image: url(&quot; + e.target.result + &quot;)&#39; data-number=&#39;&quot; + $(&quot;.upload__img-close&quot;).length + &quot;&#39; data-file=&#39;&quot; + f.name + &quot;&#39; class=&#39;img-bg&#39; &gt;&lt;div class=&#39;upload__img-close&#39;&gt;&lt;/div&gt;&lt;img  src=&#39;https://cdn-icons-png.flaticon.com/128/337/337946.png&#39;&gt;&lt;/div&gt;&lt;/div&gt;&quot;;
} else {
var html = &quot;&lt;div class=&#39;upload__img-box&#39;&gt;&lt;div style=&#39;background-image: url(&quot; + e.target.result + &quot;)&#39; data-number=&#39;&quot; + $(&quot;.upload__img-close&quot;).length + &quot;&#39; data-file=&#39;&quot; + f.name + &quot;&#39; class=&#39;img-bg&#39;&gt;&lt;div class=&#39;upload__img-close&#39;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&quot;;
}
imgWrap.append(html);
iterator++;
}
reader.readAsDataURL(f);
}
}
});
});
});
$(&#39;body&#39;).on(&#39;click&#39;, &quot;.upload__img-close&quot;, function (e) {
var file = $(this).parent().data(&quot;file&quot;);
for (var i = 0; i &lt; imgArray.length; i++) {
if (imgArray[i].name === file) {
imgArray.splice(i, 1);
break;
}
}
$(this).parent().parent().remove();
$(this).parent().children().remove();
});
}
});

<!-- language: lang-css -->
<style>
.upload__box {
padding-top: 10px;
}

.upload__inputfile {
width: 0.1px;
height: 0.1px;
opacity: 0;
overflow: hidden;
position: absolute;
z-index: -1;
}
.upload__btn {
display: inline-block;
font-weight: 600;
color: #fff;
text-align: center;
min-width: 116px;
padding: 5px;
transition: all 0.3s ease;
cursor: pointer;
border: 2px solid;
background-color: #4045ba;
border-color: #4045ba;
border-radius: 10px;
line-height: 26px;
font-size: 14px;
}
.upload__btn:hover {
background-color: unset;
color: #4045ba;
transition: all 0.3s ease;
}
.upload__btn-box {
margin-bottom: 0px;
}
.upload__img-wrap {
display: flex;
flex-wrap: wrap;
margin: 0 -10px;
}
.upload__img-box {
width: 200px;
padding: 0 10px;
margin-bottom: 0px;
}
.upload__img-close {
width: 24px;
height: 24px;
border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 10px;
right: 10px;
text-align: center;
line-height: 24px;
z-index: 1;
cursor: pointer;
}
.upload__img-close:after {
content: &#39;\2716&#39;;
font-size: 14px;
color: white;
}
.img-bg {
background-repeat: no-repeat;
background-position: center;
background-size: cover;
position: relative;
padding-bottom: 100%;
}

</style>
<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;form action=&quot;&quot;&gt;
&lt;div class=&quot;col-lg-12 mt-4&quot;&gt;
&lt;label for=&quot;file&quot;&gt;Upload Document (Multiple Document)&lt;/label&gt;
&lt;div class=&quot;upload__box&quot;&gt;
&lt;div class=&quot;upload__btn-box&quot;&gt;
&lt;label class=&quot;upload__btn&quot;&gt;
Upload file
&lt;input type=&quot;file&quot; multiple=&quot;&quot; id=&quot;files&quot; name=&quot;files[]&quot; data-max_length=&quot;20&quot; class=&quot;upload__inputfile&quot; accept=&quot;image/jpeg, image/jpg, image/png, application/pdf&quot;&gt;
&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;upload__img-wrap&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月19日 17:46:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505445.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定