HTML:
<input name="fileToUpload" type="file" id="fileToUpload"/>
<div id="div1"> </div>
Jquery:
$(function()
{
$("#fileToUpload").on('change', 'input' , function(){
// Display image on the page for viewing
readURL(this,"div1");
});
});
function readURL(input , tar) {
if (input.files && input.files[0]) { // got sth
// Clear image container
$("#" + tar ).empty();
$.each(input.files , function(index,ff) // loop each image
{
var reader = new FileReader();
// Put image in created image tags
reader.onload = function (e) {
$('#' + tar).attr('src', e.target.result);
}
reader.readAsDataURL(ff);
});
}
我希望能够将上传到 div 容器的图片显示出来,但是我的代码不起作用。有什么办法让它工作?