移动端多图片预览并上传包含php上传完整代码
移动端多图片上传并本地预览删除,PC端也适用
XML/HTML Code
- <div class="inner">
- <div class="problem">
- <div class="custom_img">
- <div class="custom_img_top">
- <p>文件限制大小10MB</p>
- <p><span class="upload_img_length">0</span>/3</p>
- </div>
- <!--点击上传图片 触发下面的div 点击事件-->
- <div class="upload_img_wrap">
- <div id="imgBox"></div>
- <img class="upload_img" data-id="1" src="img/upload_img.png" />
- <img style="display:none" class="upload_img" data-id="2" src="img/upload_img.png" />
- <img style="display:none" class="upload_img" data-id="3" src="img/upload_img.png" />
- </div>
- <div style="width: 100%;height: 100vh;position: relative;">
- <form id="upBox" class="upload_form" action="upload_batch.php" method="post" enctype="multipart/form-data">
- <div id="inputBox" style="display:none;">
- <input type="file" name="file[]" data-id="1" title="Choose file" id="file1" />
- <input type="file" name="file[]" data-id="2" title="Choose file" id="file2" />
- <input type="file" name="file[]" data-id="3" title="Choose file" id="file3" /> Click to Choose file
- </div>
- <input type="submit" name="button" id="button" value="Upload File" class="button_upload"/>
- </form>
- </div>
- </div>
- </div>
- </div>
JavaScript Code
- <script>
- var imgNum = 0;
- $(".upload_img_wrap .upload_img").bind("click", function(ev) {
- //console.log(ev.currentTarget.dataset.id)
- var index = ev.currentTarget.dataset.id;
- var that = this;
- if(index == 1) {
- $("#file1").click();
- $("#file1").unbind().change(function(e) {
- var index = e.currentTarget.dataset.id;
- if($('#file').val() == '') {
- return false;
- }
- $(that).hide();
- var filePath = $(this).val();
- changeImg(e, filePath, index);
- imgNum++;
- if(imgNum<3){
- $(".upload_img").eq(1).show();
- }
- $(".upload_img_length").html(imgNum);
- })
- } else if(index == 2) {
- $("#file2").click();
- $("#file2").unbind().change(function(e) {
- var index = e.currentTarget.dataset.id;
- if($('#file').val() == '') {
- return false;
- }
- $(that).hide();
- var filePath = $(this).val();
- changeImg(e, filePath, index);
- imgNum++;
- if(imgNum<3){
- $(".upload_img").eq(2).show();
- }
- $(".upload_img_length").html(imgNum);
- })
- } else if(index == 3) {
- $("#file3").click();
- $("#file3").unbind().change(function(e) {
- var index = e.currentTarget.dataset.id;
- if($('#file').val() == '') {
- return false;
- }
- var filePath = $(this).val();
- changeImg(e, filePath, index);
- $(that).hide();
- imgNum++;
- $(".upload_img_length").html(imgNum);
- })
- }
- })
- function changeImg(e, filePath, index) {
- fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase();
- //检查后缀名
- if(!fileFormat.match(/.png|.jpg|.jpeg|.mp4|.mov/)) {
- showError('文件格式必须为:png/jpg/jpeg');
- return;
- }
- //获取并记录图片的base64编码
- var reader = new FileReader();
- reader.readAsDataURL(e.target.files[0]);
- //alert(filePath.lastIndexOf("."));
- reader.onloadend = function() {
- // 图片的 base64 格式, 可以直接当成 img 的 src 属性值
- var dataURL = reader.result;
- // console.log(dataURL)
- // 显示图片
- if(fileFormat.indexOf(".jpg") !== -1 || fileFormat.indexOf(".png")!== -1 || fileFormat.indexOf(".jpeg")!== -1 ){
- $("#imgBox").html($("#imgBox").html() + '<div class="imgContainer" data-index=' + index + '><img src=' + dataURL + ' onclick="imgDisplay(this)"><img onclick="removeImg(this,' + index + ')" class="imgDelete" src="img/del_img.png" /></div>');
- }
- else
- {
- $("#imgBox").html($("#imgBox").html() + '<div class="imgContainer" data-index=' + index + '><img src=images/video.jpg ><img onclick="removeImg(this,' + index + ')" class="imgDelete" src="img/del_img.png" /></div>');
- }
- };
- }
- function removeImg(obj, index) {
- for(var i = 0; i < $(".imgContainer").length; i++) {
- if($(".imgContainer").eq(i).attr("data-index") == index) {
- $(".imgContainer").eq(i).remove();
- }
- }
- for(var i = 0; i < $(".upload_img").length; i++) {
- $(".upload_img").eq(i).hide();
- if($(".upload_img").eq(i).attr("data-id") == index) {
- console.log($(".upload_img").eq(i).attr("data-id"))
- $(".upload_img").eq(i).show();
- }
- }
- imgNum--;
- $(".upload_img_length").html(imgNum);
- }
- function imgDisplay(obj) {
- var src = $(obj).attr("src");
- var imgHtml = '<div style="width: 100%;height: 100vh;overflow: auto;background: rgba(0,0,0,0.5);text-align: center;position: fixed;top: 0;left: 0;z-index: 1000;display: flex;justify-content: center; align-items: center;"><img src=' + src + ' style="margin-top: 100px;width: 96%;margin-bottom: 100px;"><p style="font-size: 50px;position: fixed;top: 30px;right: 30px;color: white;cursor: pointer;" onclick="closePicture(this)">×</p></div>'
- $('body').append(imgHtml);
- }
- function closePicture(obj) {
- $(obj).parent("div").remove();
- }
- </script>
原文地址:http://www.freejs.net/article_biaodan_882.html