图像不会从文件选择器显示

时间:2014-12-26 08:30:54

标签: javascript

我想要上传一个设计的多个图像。在上传图片之前,我想看到图片的预览。点击按钮,我按照我的要求在文件,文本和下拉列表中添加控件。当我从文件选择器中选择图像时,图像不会显示在imagePreview div。



$(function() {
  $("#uploadFile").on("change", function()
                      {
    var files = !!this.files ? this.files : [];
    var image  = new Image();
    if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

    if (/^image/.test( files[0].type)){ // only image file
      var reader = new FileReader(); // instance of the FileReader
      reader.readAsDataURL(files[0]); // read the local file

      reader.onloadend = function(_file){ // set image data as background of div
        $("#imagePreview").css( { "background-image":"url("+this.result+")","background-size":"contain",  "background-repeat":"no-repeat"});
      }
    }
  });
});


function addRow() {
  var div = document.createElement('div');
  div.className = 'filerow';

  div.innerHTML = '<div class="img"><input type="file" name="image[]" id="uploadFile" multiple="multiple" style="opacity:0;-moz-opacity:0;filter:alpha(opacity:0);z-index:9999;width:90px;padding:5px;cursor:default;"/></div>\
<input class="txtupload"  type="text"  name="imgname[]" value="" id="imgname" disabled/>\
<input class="txtupload1" type="text"  name="imgtype[]" value="" id="imgtype" disabled/>\
<input class="txtupload1" type="text"  name="imgsize[]" value="" id="imgsize" disabled/>\
<select class="comupload"> <option value="No">No</option> <option value="Yes">Yes</option></select>\
<input type="button" value="Remove" onclick="removeRow(this)">';

  document.getElementById('added_files_box').appendChild(div);
}

function removeRow(input) 
{
  document.getElementById('added_files_box').removeChild( input.parentNode );
}
&#13;
    #imagePreview {
        width: 150px;
        height: 150px;
        background-position: center center;
        background-size: cover;
        -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);
        display: inline-block;
    }
    
    .img{ font-weight:bold; 
          font-size:12px;font-family:Arial, Helvetica, sans-serif;
          text-align:center;
          background:#f2f2f2 url('images/browse_file_by_vasplus_programming_blog.png') no-repeat 12px 9px;
          color:green;border:1px solid #ccc;height:30px;cursor: default;width:106px;-moz-border-radius:5px; 
          -webkit-border-radius:5px;
          float:left; 
        }
&#13;
    <div style="border:2px; width:1038px; height:280px; margin:0; float:left;">
        <fieldset style="height:270px; width:1035px;"> 
            <legend><b>Design Image Detail</b></legend>   
            <div style="border:2px solid; width:738px; height:270px; margin:0; float:left;">
                <center>
                <div id="added_files_box" class="file_upload_main">
                </div>
                
                <input type="button" value="Add more data" onclick="addRow()"/>
                </center>
            </div>
            
            <div id="imagePreview" style="border:2px solid; width:250px; height:270px; margin:0; float:left;">
            
            </div>
        </fieldset>                                    
    </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您可以参考以下示例:

  • 我没有使用on事件,而是更改它以在添加新行时添加事件
  • 除此之外,我将输入ID更改为class,因为ID在HTML
  • 中应该是唯一的

示例JavaScript:

function addRow() {
            var div = document.createElement('div');
            div.className = 'filerow';

            div.innerHTML = '<div class="img"><input type="file" name="image[]" class="uploadFile" multiple="multiple" style="opacity:0;-moz-opacity:0;filter:alpha(opacity:0);z-index:9999;width:90px;padding:5px;cursor:default;"/></div>\
                <input class="txtupload"  type="text"  name="imgname[]" value="" class="imgname" disabled/>\
                <input class="txtupload1" type="text"  name="imgtype[]" value="" class="imgtype" disabled/>\
                <input class="txtupload1" type="text"  name="imgsize[]" value="" class="imgsize" disabled/>\
                <select class="comupload"> <option value="No">No</option> <option value="Yes">Yes</option></select>\
                <input type="button" value="Remove" onclick="removeRow(this)">';

            document.getElementById('added_files_box').appendChild(div);
            addEvent(div);
        }

        function removeRow(input) {
            document.getElementById('added_files_box').removeChild(input.parentNode);
        }

        function addEvent(div) {
            $(div).find('.uploadFile').change(function() {
                var files = !! this.files ? this.files : [];
                var image = new Image();
                if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

                if (/^image/.test(files[0].type)) { // only image file
                    var reader = new FileReader(); // instance of the FileReader
                    reader.readAsDataURL(files[0]); // read the local file

                    reader.onloadend = function (_file) { // set image data as background of div
                        $("#imagePreview").css({
                            "background-image": "url(" + this.result + ")",
                                "background-size": "contain",
                                "background-repeat": "no-repeat"
                        });
                    }
                }
            });
        }

希望这有帮助:D