如何为在fileuplaod中选择的多个图像提供预览使用Jquery?

Sof*_*erd 2 jquery

大家好我有一个fileuplaod用户可以选择多个图像我想在上传之前显示所选图像的预览...目前我管它为单个图像预览如何为所选的多个图像提供预览

  function readURL(input) {
    var img = $(input).closest('div').find('img').first();
    var imgid=$(img).attr('id');
    if (input.files && input.files[0]) {
        alert(input.files);
        alert(input.files[0]);
        var reader = new FileReader();

        reader.onload = function (e) {
            $("#"+imgid)
                .attr('src', e.target.result);
        };

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

 <input type="file"  accept="gif|jpg|jpeg|png" name="files[]" multiple onchange="readURL(this);" />

                    <img src="http://placehold.it/140x140" class="img-rounded" id="thumbnailimage">
Run Code Online (Sandbox Code Playgroud)

请有人帮忙吗...

dav*_*rty 11

好的,这是一个非常粗略的实现

基本的想法是,获取文件数组,循环遍历它,使用File API添加图像,其中src值是js给你玩的blob,而不是用户机器上的路径.

var inputLocalFont = document.getElementById("image-input");
inputLocalFont.addEventListener("change",previewImages,false); //bind the function to the input

function previewImages(){
    var fileList = this.files;

    var anyWindow = window.URL || window.webkitURL;

        for(var i = 0; i < fileList.length; i++){
          //get a blob to play with
          var objectUrl = anyWindow.createObjectURL(fileList[i]);
          // for the next line to work, you need something class="preview-area" in your html
          $('.preview-area').append('<img src="' + objectUrl + '" />');
          // get rid of the blob
          window.URL.revokeObjectURL(fileList[i]);
        }


}
Run Code Online (Sandbox Code Playgroud)