2016-01-11 113 views
10

我試圖用jQuery做多個輸入圖像預覽。預覽圖像工作正常,但在同一個div中的圖像預覽,我需要在不同的div中的每個圖像預覽。我該怎麼做?jQuery的圖像預覽

HTML

<html> 
    <form action="index.php" id="myForm" name="myForm" enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
    desktop:<input type="file" id="files" name="files" multiple><br/> 
      <div id="selectedFiles"></div> 
     mobile:<input type="file" id="mobile" name="mobile" multiple><br/> 
      <div id="selectFiles"></div> 
    <button type="submit">Submit</button> 
    </form></br></body> 
</html> 

custom.js

$(document).ready(function() { 
/*multiple image preview first input*/ 

    $("#files").on("change", handleFileSelect); 

    selDiv = $("#selectedFiles"); 
    $("#myForm").on("submit", handleForm); 

    $("body").on("click", ".selFile", removeFile); 

    /*end image preview */ 

    /* Multiple image preview second input*/ 
    $("#mobile").on("change", handleFileSelect); 

    selDivM = $("#selectFiles"); 
    $("#myForm").on("submit", handleForm); 

    $("body").on("click", ".selFile", removeFile); 

    /*end image preview*/ 
)} 

方法
功能處理文件選擇

var selDiv = ""; 
var selDivM=""; 
var storedFiles = []; 

function handleFileSelect(e) { 
    var files = e.target.files; 
    var filesArr = Array.prototype.slice.call(files); 
    filesArr.forEach(function(f) { 

    if(!f.type.match("image.*")) { 
     return; 
    } 
    storedFiles.push(f); 

    var reader = new FileReader(); 
    reader.onload = function (e) { 
    var html = "<div><img src=\"" + e.target.result + "\"  data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>"; 
     if (typeof selDivM !== "") { 
      selDivM.append(html); 
     }else { 
      selDiv.append(html); 
     } 

    } 
    reader.readAsDataURL(f); 
}); 

} 

功能處理表單

function handleForm(e) { 
e.preventDefault(); 
var data = new FormData(); 

for(var i=0, len=storedFiles.length; i<len; i++) { 
    data.append('files', storedFiles[i]); 
} 

var xhr = new XMLHttpRequest(); 
xhr.open('POST', 'handler.cfm', true); 

xhr.onload = function(e) { 
    if(this.status == 200) { 
     console.log(e.currentTarget.responseText); 
     alert(e.currentTarget.responseText + ' items uploaded.'); 
    } 
} 

xhr.send(data); 
} 

功能刪除文件,如果點擊

function removeFile(e) { 
    var file = $(this).data("file"); 
    for(var i=0;i<storedFiles.length;i++) { 
    if(storedFiles[i].name === file) { 
     storedFiles.splice(i,1); 
     break; 
    } 
} 
$(this).parent().remove(); 
} 

see code pen snippet

$(document).ready(function() { 
 
    /*multiple image preview first input*/ 
 

 
    $("#files").on("change", handleFileSelect); 
 

 
    selDiv = $("#selectedFiles"); 
 
    $("#myForm").on("submit", handleForm); 
 

 
    $("body").on("click", ".selFile", removeFile); 
 

 
    /*end image preview */ 
 

 
    /* Multiple image preview second input*/ 
 
    $("#mobile").on("change", handleFileSelect); 
 

 
    selDivM = $("#selectFiles"); 
 
    $("#myForm").on("submit", handleForm); 
 

 
    $("body").on("click", ".selFile", removeFile); 
 
}); 
 
    /*multiple image preview*/ 
 

 

 
var selDiv = ""; 
 
// var selDivM=""; 
 
var storedFiles = []; 
 

 
function handleFileSelect(e) { 
 
    var files = e.target.files; 
 
    var filesArr = Array.prototype.slice.call(files); 
 
    filesArr.forEach(function(f) { 
 

 
     if(!f.type.match("image.*")) { 
 
      return; 
 
     } 
 
     storedFiles.push(f); 
 

 
     var reader = new FileReader(); 
 
     reader.onload = function (e) { 
 
      var html = "<div><img src=\"" + e.target.result + "\" data-file='"+f.name+"' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>"; 
 
      selDivM.append(html); 
 
      } 
 
     reader.readAsDataURL(f); 
 
    }); 
 

 
} 
 

 
function handleForm(e) { 
 
    e.preventDefault(); 
 
    var data = new FormData(); 
 

 
    for(var i=0, len=storedFiles.length; i<len; i++) { 
 
     data.append('files', storedFiles[i]); 
 
    } 
 

 
    var xhr = new XMLHttpRequest(); 
 
    xhr.open('POST', 'handler.cfm', true); 
 

 
    xhr.onload = function(e) { 
 
     if(this.status == 200) { 
 
      console.log(e.currentTarget.responseText); 
 
      alert(e.currentTarget.responseText + ' items uploaded.'); 
 
     } 
 
    } 
 

 
    xhr.send(data); 
 
} 
 

 
function removeFile(e) { 
 
    var file = $(this).data("file"); 
 
    for(var i=0;i<storedFiles.length;i++) { 
 
     if(storedFiles[i].name === file) { 
 
      storedFiles.splice(i,1); 
 
      break; 
 
     } 
 
    } 
 
    $(this).parent().remove(); 
 
}
#selectedFiles img { 
 
      max-width: 200px; 
 
      max-height: 200px; 
 
      float: left; 
 
      margin-bottom:10px; 
 
     } 
 
     #userActions input{ 
 
      width: auto; 
 
      margin: auto; 
 
     } 
 
     #selectFiles img { 
 
      max-width: 200px; 
 
      max-height: 200px; 
 
      float: left; 
 
      margin-bottom:10px; 
 
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> 
 
<html> 
 
    <body> 
 
    <form action="index.php" id="myForm" name="myForm" enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
 
    desktop:<input type="file" id="files" name="files" multiple><br/> 
 
      <div id="selectedFiles"></div> 
 
     mobile:<input type="file" id="mobile" name="mobile" multiple><br/> 
 
      <div id="selectFiles"></div> 
 
    <button type="submit">Submit</button> 
 
    </form> 
 
    </body> 
 
</html>

+0

您可以接收數據的圖片src ? – Muhammed

+0

你可以在你的函數'handleFileSelect(e)'中使用'e.target.name'這個名字,它會返回'files'或'mobile'。根據您的需要,您可以將其附加到div上。 –

+0

你好,我只是想知道如何刪除功能在這裏工作?它不適合我的代碼。這需要Handler.cfm嗎? – Sravan

回答

2

這是基於你的剪斷....創建一個基於設備類型的預覽一個div

$(document).ready(function() { 
 
    /*multiple image preview first input*/ 
 

 
    $("#files").on("change", handleFileSelect); 
 

 
    selDiv = $("#selectedFiles"); 
 
    $("#myForm").on("submit", handleForm); 
 

 
    $("body").on("click", ".selFile", removeFile); 
 

 
    /*end image preview */ 
 

 
    /* Multiple image preview second input*/ 
 
    $("#mobile").on("change", handleFileSelect); 
 

 
    selDivM = $("#selectFilesM"); 
 
    $("#myForm").on("submit", handleForm); 
 

 
    $("body").on("click", ".selFile", removeFile); 
 

 
    console.log($("#selectFilesM").length); 
 
}); 
 
/*multiple image preview*/ 
 

 

 
var selDiv = ""; 
 
// var selDivM=""; 
 
var storedFiles = []; 
 

 
function handleFileSelect(e) { 
 

 
    var files = e.target.files; 
 
    var filesArr = Array.prototype.slice.call(files); 
 
    var device = $(e.target).data("device"); 
 
    filesArr.forEach(function(f) { 
 

 
    if (!f.type.match("image.*")) { 
 
     return; 
 
    } 
 
    storedFiles.push(f); 
 

 
    var reader = new FileReader(); 
 
    reader.onload = function(e) { 
 
     var html = "<div><img src=\"" + e.target.result + "\" data-file='" + f.name + "' class='selFile' title='Click to remove'>" + f.name + "<br clear=\"left\"/></div>"; 
 

 
     if (device == "mobile") { 
 
     $("#selectedFilesM").append(html); 
 
     } else { 
 
     $("#selectedFiles").append(html); 
 
     } 
 
    } 
 
    reader.readAsDataURL(f); 
 
    }); 
 

 
} 
 

 
function handleForm(e) { 
 
    e.preventDefault(); 
 
    var data = new FormData(); 
 

 
    for (var i = 0, len = storedFiles.length; i < len; i++) { 
 
    data.append('files', storedFiles[i]); 
 
    } 
 

 
    var xhr = new XMLHttpRequest(); 
 
    xhr.open('POST', 'handler.cfm', true); 
 

 
    xhr.onload = function(e) { 
 
    if (this.status == 200) { 
 
     console.log(e.currentTarget.responseText); 
 
     alert(e.currentTarget.responseText + ' items uploaded.'); 
 
    } 
 
    } 
 

 
    xhr.send(data); 
 
} 
 

 
function removeFile(e) { 
 
    var file = $(this).data("file"); 
 
    for (var i = 0; i < storedFiles.length; i++) { 
 
    if (storedFiles[i].name === file) { 
 
     storedFiles.splice(i, 1); 
 
     break; 
 
    } 
 
    } 
 
    $(this).parent().remove(); 
 
}
#selectedFiles img, 
 
#selectFilesM img { 
 
    max-width: 200px; 
 
    max-height: 200px; 
 
    float: left; 
 
    margin-bottom: 10px; 
 
} 
 
#userActions input { 
 
    width: auto; 
 
    margin: auto; 
 
} 
 
#selectFiles img, 
 
#selectFilesM img { 
 
    max-width: 200px; 
 
    max-height: 200px; 
 
    float: left; 
 
    margin-bottom: 10px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form action="index.php" id="myForm" name="myForm" enctype="multipart/form-data" method="post" accept-charset="utf-8"> 
 
    desktop: 
 
    <input data-device="desktop" type="file" id="files" name="files" multiple> 
 
    <div id="selectedFiles"></div> 
 
    <br/>mobile: 
 
    <input data-device="mobile" type="file" id="mobile" name="mobile" multiple> 
 
    <br/> 
 

 
    <div id="selectedFilesM"></div> 
 
    <button type="submit">Submit</button> 
 
</form>

2

您實現這一目標的下面的代碼片段

$("#fileUploadInput").change(function(event){ 
 
    $("#filePreviewImage").attr('src',URL.createObjectURL(event.target.files[0])); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="fileUploadInput" type="file"> 
 
<img id="filePreviewImage" src="" alt="The image preview" height="90" width="160">

只需更換"filePreviewImage"your custom selector,它將填充預覽不分股利。 享受 ...

+0

這是單幅圖像預覽,我需要多個圖像預覽。 –

+0

您正在使用多個文件輸入?你是什​​麼意思多個圖像預覽 –

+0

請看我的片段。 –