IE上的图像上传预览太慢了

时间:2017-03-14 08:45:41

标签: javascript jquery html filereader

这是我的代码。它在Chrome上工作正常但在IE上却太慢了。我试过使用javascript而不是jquery但没有改变!

每个人都说innerHTML比追加更快。但是当我使用innerHTML而不是追加时,没有任何改变。我真的需要一些建议。 感谢

 <input id="uploadPhoto" name="uploadPhoto" type="file" multiple accept="image/*" />

                $("#uploadPhoto").on('change', function () {
                    var $this = $(this);
                    var countOfFiles = $this[0].files.length;
                    var maxSize = countOfFiles * 5242880;
                    if($this[0].files[0].size > maxSize)
                    {
                        $.smallBox({
                            title : "too big size",
                            color : "#c69120",
                            iconSmall : "fa fa-warning fa-2x fadeInRight animated",
                            timeout : 9000
                        });
                        $('#uploadPhoto').val('');
                    }
                    else
                    {

                        var input = document.getElementById('uploadPhoto');
                        var files = $this[0].files;
                        var imgPath = $this[0].value;
                        var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
                        var image_holder = $("#image-holder");
                        image_holder.empty();
                        if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
                            if (typeof (FileReader) != "undefined") {
                                var i = 0,
                                len = files.length;
                                (function readFile(n) {
                                    var reader = new FileReader();
                                    var f = files[n];

                                    storedPhotos.push(files[n]);
                                    var ustdiv ='<div class="postimage col-md-12">'
                                    reader.onload = function (e) {
                                        var spanDiv = "<div class='col-md-3 col-lg-3 col-sm-4 col-xs-6 no-padding tempFile'><span class='close closeselected'>&times;</span>";
                                        var imageDiv = "<img class='img-responsive' data-id='" + f.name + "' src='" + e.target.result + "'/></div>";
                                        var sonDiv = spanDiv + imageDiv +'</div>';
                                        $(sonDiv).appendTo(image_holder);

                                        if (n < len - 1) readFile(++n)
                                    };
                                    reader.readAsDataURL(f);
                                }(i));

                            } else {
                                alert("This browser does not support FileReader.");
                            }
                        } else {
                            alert("Only image files!");
                        }
                    }
                });

1 个答案:

答案 0 :(得分:4)

长时间的原因是二进制数据必须转换,编码(FileReader.readAsDataURL())和解码(Image)到字符串格式(这里:Base-64),这在IE中很慢。保持“过程链”二进制将加速处理显着

因此,请将BlobBlob-URLs一起使用。这将更快地工作,并在所有浏览器including IE中使用更少的内存。这也消除了FileReader,因为我们可以直接使用File对象,因为它只是一个Blob的子类。

document.getElementById("i").onchange = function() {

  // create Blob-URL for File (=Blob) object
  var url = (URL || webkit).createObjectURL(this.files[0]);
  
  // set Blob-URL as image source:
  document.getElementById("img").src = url;
};
<label>Select image: <input id=i type=file></label><br>
<img id=img>

根据需要应用于您的方案。

document.getElementById("i").onchange = function() {

  // multiple image files
  for(var i=0; i < this.files.length; i++) {
    var img = new Image();
    img.title = this.files[i].name; // hover the image to see name
    img.onload = cleanup;           // release memory locked by Blob-URL
    img.src = (URL || webkit).createObjectURL(this.files[i]);
    document.body.appendChild(img);
  }
  function cleanup() {(URL || webkit).revokeObjectURL(this.src)};
};
img {height:128px;width:auto}
<label>Select image: <input id=i type=file multiple></label><br>