简体   繁体   English

使用JavaScript从浏览器中拖出文件

[英]Drag out files from browser using javascript

I'm trying to modify some code for dragging out files from your browser window I found here: http://www.thecssninja.com/javascript/gmail-dragout 我正在尝试修改一些代码,以从您在此处找到的浏览器窗口中拖出文件: http : //www.thecssninja.com/javascript/gmail-dragout

In my code I want to use for loops so I can handle a large number of files without repeating code over and over. 在我的代码中,我想使用循环,这样我就可以处理大量文件,而不必一遍又一遍地重复代码。

This is what I have: 这就是我所拥有的:

<a href = "file0" id = "0" draggable = "true" data-downloadurl="application/pdf:file0name.pdf:file:///C:/filepath/file0.pdf">file0</a>
<a href = "file1" id = "1" draggable = "true" data-downloadurl="application/pdf:file1name.pdf:file:///C:/filepath/file1.pdf">file1</a>
<a href = "file2" id = "2" draggable = "true" data-downloadurl="application/pdf:file2name.pdf:file:///C:/filepath/file2name.pdf">file2</a>
<a href = "file3" id = "3" draggable = "true" data-downloadurl="application/pdf:file3name.pdf:file:///C:/filepath/file3name.pdf">file3</a>

<script type = "text/javascript>
    var file_count = 3;
var fileDetails = [];

var files = new Array(file_count);
for(var i=0; i<=file_count; i++) {
    files[i] = document.getElementById(i);
}

if(typeof files[0].dataset === "undefined") {
    for (i=0; i<=file_count; i++) {
        fileDetails[i] = files[i].getAttribute("data-downloadurl");
    }
}else {
    for (i=0; i<=file_count; i++) {
        fileDetails[i] = files[i].dataset.downloadurl;
    }
}

//I'm pretty sure the problem starts here.
//If you substitue i for a specific element from the array and comment out the for loop, the script works just fine for the element specified.
for(i=0; i<=file_count; i++) {
    files[i].addEventListener("dragstart",function(evt){
        evt.dataTransfer.setData("DownloadURL",fileDetails[i]);
    },false);
}

I'm fairly certain the problem starts where I have it labeled, I'm not sure why there is a problem or how to solve it. 我很确定问题是从我贴上标签的地方开始的,我不确定为什么有问题或如何解决。

Some things I should point out: 我应该指出一些事情:

This only works in Chrome. 这仅适用于Chrome。 This is not an issue for me. 这对我来说不是问题。

I want this to handle 20+ files. 我希望它可以处理20多个文件。

The problem does indeed start where you think it does 问题确实从您认为的地方开始

//I'm pretty sure the problem starts here.
//If you substitue i for a specific element from the array and comment out the for loop, the script works just fine for the element specified.
for(i=0; i<=file_count; i++) {
    files[i].addEventListener("dragstart",function(evt){
        evt.dataTransfer.setData("DownloadURL",fileDetails[i]);
    },false);
}

Basically your dragstart event listener is bound to i , which at the time of execution is the last element in the list. 基本上,您的dragstart事件侦听器绑定到i ,它在执行时是列表中的最后一个元素。

The following will work better. 以下将更好地工作。

//I'm pretty sure the problem starts here.
//If you substitue i for a specific element from the array and comment out the for loop, the script works just fine for the element specified.
for(i=0; i<=file_count; i++) {
    files[i].addEventListener("dragstart", (function(idx) {
        return function(evt){
           evt.dataTransfer.setData("DownloadURL",fileDetails[idx]);
        }
    })(i),false);
}

In the above code you will be creating and returning an object of type function with the correct value of i bound - essentially the closure is created at a different level and thus when accessed will get the correct data. 在上面的代码中,您将创建并返回具有i bound正确值的函数类型的对象-本质上,闭包是在不同级别创建的,因此在访问时将获取正确的数据。

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

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