javascript - How to get files by dragging them to the browser?
高洛峰
高洛峰 2017-06-12 09:25:02
0
1
704

If you click to upload on the page, you can click to get the files[0] in the input file;

The current requirement is to drag the image directly from the desktop to the browser. How to get the first file in files[0]?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
typecho
/**
 * 拖拽实现
 */
var dropbox = document.getElementById("dropbox");

dropbox.addEventListener("dragenter", function(e){
    e.stopPropagation();
    e.preventDefault();
}, false);
            
dropbox.addEventListener("dragover", function(e){
    e.stopPropagation();
    e.preventDefault();
}, false);
            
dropbox.addEventListener("drop", function(e){
    e.stopPropagation();
    e.preventDefault();
            
    var dt = e.dataTransfer;
    var files = dt.files;
    
    // files[0]即为第一个文件
      
}, false);

For details, please refer to: Selecting files through drag and drop operations

I also recommend you an example I wrote:
Select image files - supports three input methods (input box selection, drag and drop selection, screenshot and paste three input methods)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template