简体   繁体   English

使用HTML5和Javascript从桌面上传(拖放)

[英]Upload from desktop with HTML5 and Javascript (drag and drop)

I'm making a typical drag and drop for upload images from desktop to browser, i can drop content inside the box and with a console.log view the content in the browser console: 我正在进行典型的拖放操作,以将图像从桌面上传到浏览器,我可以将内容拖放到框内,并使用console.log在浏览器控制台中查看内容:

File { name: "deam230mthumb.jpg", lastModified: 1194641808000, lastModifiedDate: Date 2007-11-09T20:56:48.000Z, size: 60313, type: "image/jpeg" }

I want to view the image inside a box and then upload on submit. 我想在一个框内查看图像,然后在提交时上传。

Here my code is use Jade template engine: 这里我的代码是使用Jade模板引擎:

Jade (HTML) 玉(HTML)

form(action="" enctype="multipart/form-data")
 div(class="all-100 miniatures")
 div(class="all-100 drop ink-droppable align-center fallback" id="dropZone" ondrop="drop(event)" ondragover="allowDrop(event)")

Javascript Java脚本

script.
      var dropZone = document.getElementById('dropZone');
      function allowDrop(e){
        e.preventDefault();
      }

      function drop(e){
        var file = e.dataTransfer.files[0];
        e.preventDefault();
        console.log(file);
        //e.target.appendChild(file);

      }

Ok, my Javascript looks this now: 好的,我的Javascript现在看起来像这样:

At this point we can drag images from our desktop to browser and view them, now, we need to upload that files. 此时,我们可以将图像从桌面拖动到浏览器并查看它们,现在,我们需要上传该文件。

script.
      var dropZone = document.getElementById('dropZone');
      function allowDrop(e){
        e.preventDefault();
      }

      function drop(e){
        var file = e.dataTransfer.files[0];
        e.preventDefault();
        console.log(file);
        var reader = new FileReader();
        reader.onload = function(event){
          var miniatures = document.getElementById('miniatures');
          var miniature = new Image();
          miniature.src = event.target.result;
          miniature.width = 100;
          miniatures.appendChild(miniature);
        }
        reader.readAsDataURL(file);
      }

I added an array for content each file from the event, on the following code we need to add a controller for handle each file from the array with a register of each one on the database. 我为事件中每个文件的内容添加了一个数组,在下面的代码上,我们需要添加一个控制器来处理数组中的每个文件,并在数据库中添加每个寄存器。

script.
      var dropZone = document.getElementById('dropZone');
      function allowDrop(e){
        e.preventDefault();
      }
      var files = [];
      function drop(e){
        var file = e.dataTransfer.files[0];
        files.push(file);
        console.log(file)
        e.preventDefault();

        var reader = new FileReader();
        reader.onload = function(event){
          var miniatures = document.getElementById("miniatures");      
          var img = new Image();
          img.src = event.target.result;
          var miniature = document.createElement("div");
          miniature.className = "all-20";
          miniature.appendChild(img);
          miniatures.appendChild(miniature);               
        }
        var readerContent = reader.readAsDataURL(file);
        var input = document.getElementById("upload");
        input.files = readerContent;
        console.log(files);
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM