2016-10-02 57 views
3

如何使用javascript在html中上傳圖片時顯示多個縮略圖?

function readURL(input) { 
 
    if (input.files && input.files[0]) { 
 
    var reader = new FileReader(); 
 

 
    reader.onload = function(e) { 
 
     $('#documentUpload') 
 
     .attr('src', e.target.result) 
 

 
    }; 
 

 
    reader.readAsDataURL(input.files[0]); 
 
    } 
 
}
<html> 
 

 
<head></head> 
 

 
<body> 
 
    <ul> 
 
    <li> 
 
     <input type='file' onchange="readURL(this);" /> 
 
     <img id="documentUpload" src="#" alt="first image" /> 
 
    </li> 
 

 
    <li> 
 
     <input type='file' onchange="readURL(this);" /> 
 
     <img id="documentUpload" src="#" alt="second image" /> 
 
    </li> 
 
    </ul> 
 
</body> 
 

 
</html> 
 

 
> Blockquote

「在例如,單擊任意選擇圖像按鈕,但圖像會顯示在只有第一種情況下,我在這兩種情況下,並在JavaScript中改變了ID,以及,但它沒有工作。 代碼以上是如何在html中顯示圖像的解決方案「

回答

2

問題是ID需要唯一。在這個例子中,我添加了一個名爲document-up的屬性,它起作用。在這種情況下,可以使用屬性或類來選擇多個元素。

function readURL(input,option) { 
 
    if (input.files && input.files[0]) { 
 
    var reader = new FileReader(); 
 

 
    reader.onload = function(e) { 
 
     if (option == 1){ 
 
     $("#documentUpload1") 
 
     .attr('src', e.target.result) 
 
     } else { 
 
     $("#documentUpload2") 
 
     .attr('src', e.target.result) 
 
     } 
 
    }; 
 

 
    reader.readAsDataURL(input.files[0]); 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<html> 
 

 
<head></head> 
 

 
<body> 
 
    <ul> 
 
    <li> 
 
     <input id="input1" type='file' onchange="readURL(this,1);" /> 
 
     <img id="documentUpload1" document-up src="#" alt="first image" /> 
 
    </li> 
 

 
    <li> 
 
     <input id="input2" type='file' onchange="readURL(this,2);" /> 
 
     <img id="documentUpload2" document-up src="#" alt="second image" /> 
 
    </li> 
 
    </ul> 
 
</body> 
 

 
</html>

+0

它的工作,但相同的圖像越來越uploaded.I希望不同的圖像 – VipC

+0

@ user31502,我編輯的代碼,你的情況,傳遞一個選項,以找到圖像的目標。 –

+0

謝謝,對不起延遲迴復 – VipC

2

這裏有一個方法,將用於圖像的任意數量和每個文件選擇器圖像任意數量的工作。

您只需將#previewHolder div封裝爲表單並處理其提交。

function newEl(tag){return document.createElement(tag)} 
 
function byId(id){return document.getElementById(id)} 
 
function allByTag(tag,parent){return (parent == undefined ? document : parent).getElementsByTagName(tag)} 
 

 

 
// useful for HtmlCollection, NodeList, String types 
 
function forEach(array, callback, scope){for (var i=0,n=array.length; i<n; i++)callback.call(scope, array[i], i, array);} // passes back stuff we need 
 

 
// callback gets data via the .target.result field of the param passed to it. 
 
function loadFileObject(fileObj, loadedCallback){var a = new FileReader();a.onload = loadedCallback;a.readAsDataURL(fileObj);} 
 

 

 
window.addEventListener('load', onDocLoaded, false); 
 

 
function onDocLoaded(evt) 
 
{ 
 
\t byId('addBtn').addEventListener('click', onAddBtnClicked, false); 
 
} 
 

 

 
/* html below function needs to create - we dont bother with the img here, since we create as needed when file/s picked */ 
 
/* 
 
\t \t <div class='item'> 
 
\t \t \t <img height='100px' width='100px'/><br> 
 
\t \t \t <input type='file'/> 
 
\t \t </div> 
 
*/ 
 
function onAddBtnClicked(evt) 
 
{ 
 
\t var wrapper = newEl('div'); 
 
\t wrapper.className = 'item'; 
 
// \t var img = newEl('img'); 
 
// \t img.style.height = '100px'; 
 
// \t wrapper.appendChild(img); 
 

 
\t var input = newEl('input') 
 
\t input.type = 'file'; 
 
\t 
 
\t // input.multiple = 'true'; \t \t \t // file-inputs are single-selection only be default. 
 
\t 
 
\t input.addEventListener('change', onFileChanged, false); 
 
\t input.name = 'inputFiles[]'; \t \t \t // all inputs to get same name. Name to include [] so php can retrieve all files 
 
\t wrapper.appendChild(input); 
 
\t 
 
\t byId('previewHolder').appendChild(wrapper); 
 
} 
 

 
function onFileChanged(evt) 
 
{ 
 
\t var numFiles = this.files.length; 
 
\t var itemWrapper = this.parentNode; 
 
\t var fileInput = this; 
 
\t 
 
\t if (numFiles == 0) 
 
\t { 
 
\t \t // no files chosen, so remove this preview/file-picker element 
 
\t \t var previewHolder = itemWrapper.parentNode; 
 
\t \t previewHolder.removeChild(itemWrapper); 
 
\t } 
 
\t else 
 
\t { 
 
\t \t // remove all/any existing images 
 
\t \t while (allByTag('img', itemWrapper).length != 0) 
 
\t \t \t itemWrapper.removeChild(allByTag('img', itemWrapper)[0]); 
 
\t \t \t 
 
\t \t forEach(this.files, loadAndPreviewImage); 
 
\t \t 
 
\t \t function loadAndPreviewImage(fileObj) 
 
\t \t { 
 
\t \t \t loadFileObject(fileObj, onFileObjLoaded); 
 
\t \t \t 
 
\t \t \t function onFileObjLoaded(evt) \t //.target.result; 
 
\t \t \t { 
 
\t \t \t \t var img = newEl('img'); 
 
\t \t \t \t img.style.height = '100px'; 
 
\t \t \t \t img.src = evt.target.result; 
 
\t \t \t \t itemWrapper.insertBefore(img, fileInput); 
 
\t \t \t } 
 
\t \t } 
 
\t } 
 
}
.item 
 
{ 
 
\t border: solid 1px black; 
 
\t border-radius: 6px; 
 
\t padding: 4px; 
 
} 
 
.button:hover 
 
{ 
 
\t background-color: #b0ffb0; 
 
    cursor: pointer; 
 
}
\t <div id='previewHolder' style='width: 200px'> 
 
\t \t <div class='button' id='addBtn' style='text-align:center;padding: 4px'><svg xmlns="http://www.w3.org/2000/svg" height="32" width="32" viewBox="0 0 32 32"> 
 
\t <g transform="translate(0 -1020)" stroke="#00c03b" fill="none"> 
 
\t \t <circle cx="16" cy="1036" r="14.5" stroke-width="2.998"/> 
 
\t \t <path d="m8 1036h16" stroke-linecap="round" stroke-width="3"/> 
 
\t \t <path d="m16 1044v-16" stroke-linecap="round" stroke-width="3"/> 
 
\t </g> 
 
</svg></div> 
 
\t </div>

相關問題