【问题标题】:Display Image after drag and drop on html page在html页面上拖放后显示图像
【发布时间】:2013-12-05 15:21:32
【问题描述】:

我正在使用 JQuery/JS 实现拖放图像控制。已经完成的是用户将一些图像文件拖放到html页面中。删除文件后,我只显示文件名及其大小。

我还需要在页面上显示图像。但我无法在 jquery 中获取图像文件内容。

以下是我写的代码:

function handleFileUpload(files, obj) {
for (var i = 0; i < files.length; i++) {
    var fd = new FormData();
    fd.append('file', files[i]);

    var status = new createStatusbar(obj); //Using this we can set progress.
    status.setFileNameSize(files[i].name, files[i].size);
    sendFileToServer(fd, status);

}
}
$(document).ready(function () {
var obj = $("#dragandrophandler");
obj.on('dragenter', function (e) {
    e.stopPropagation();
    e.preventDefault();
    $(this).css('border', '2px solid #0B85A1');
});
obj.on('dragover', function (e) {
    e.stopPropagation();
    e.preventDefault();
});
obj.on('drop', function (e) {

    $(this).css('border', '2px dotted #0B85A1');
    e.preventDefault();
    var files = e.originalEvent.dataTransfer.files;

    //We need to send dropped files to Server
    handleFileUpload(files, obj);
});
$(document).on('dragenter', function (e) {
    e.stopPropagation();
    e.preventDefault();
});
$(document).on('dragover', function (e) {
    e.stopPropagation();
    e.preventDefault();
    obj.css('border', '2px dotted #0B85A1');
});
$(document).on('drop', function (e) {
    e.stopPropagation();
    e.preventDefault();
});

});

在调试功能handleFileUpload上,它只显示以下属性:

您能在这方面提供帮助吗?

谢谢

【问题讨论】:

    标签: javascript jquery html drag-and-drop


    【解决方案1】:

    我还没有尝试过,但您正在使用HTML5 File API。您的图像是File 类的实例化,它本身是Blob 的子类。使用FileReader 您可以访问文件的内容。 MDN 上的这篇文章链接到网络中的另一篇文章,该文章展示了如何使用它:Showing thumbnails of User-selected images

    基本上,您可以像这样访问内容:

    function handleFileUpload( files, obj ) {
        for( var i = 0; i < files.length; i++ ) {
            // ... your other code
            var reader = new FileReader();
            reader.onload = function(){
                // Should look like data:,<jibberish_data> based on which method you called
                console.log(e.target.result);
            };
            reader.readAsDataURL(files[i]);
        }
    }
    

    【讨论】:

      【解决方案2】:

      所以,这是我解决问题的方法:

      function handleFileUpload(files, obj) {
      for (var i = 0; i < files.length; i++) {
          var fd = new FormData();
          fd.append('file', files[i]);
          var status = new createStatusbar(obj); //Using this we can set progress.
          //status.setFileNameSize(files[i].name, files[i].size);
          //sendFileToServer(fd, status);
      
          var list = document.getElementById("image-list");
          var cell = document.createElement("td");
          var img = document.createElement("img");
          img.classList.add("obj");
          img.file = files[i];
          cell.setAttribute("align", "center");
          cell.setAttribute("valign", "bottom");
          cell.appendChild(img);
          list.appendChild(cell);
      
          var reader = new FileReader();
          reader.onload = (function (aImg) { return function (e) { aImg.src = e.target.result; }; })(img);
          reader.readAsDataURL(files[i]);
      }
      }
      

      【讨论】:

      • 这对我有用。关键是您可以通过编程方式将 img.src 设置为加载事件返回的数据。谢谢!
      • 哇,那个黑魔法 reader.onload = line.. :D 我没能把它转换成现代语法 :D 但有效!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2017-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多