最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to get the file URL from ondrop event in javascript - Stack Overflow

programmeradmin4浏览0评论

I'm running the following code for an uploader I'm making.

holder.ondrop = function (e) {
    e.preventDefault();
    console.log(e);
}

I want the user to be able to drag a file from the desktop onto the web browser, and then I want to capture it's location, so that I can manually upload it (Don't want to do the uploading through javascript) My question is though, how do I get the clients location of the file from the event, so that I can put it in a <input type="file"> ?

Thanks.

I'm running the following code for an uploader I'm making.

holder.ondrop = function (e) {
    e.preventDefault();
    console.log(e);
}

I want the user to be able to drag a file from the desktop onto the web browser, and then I want to capture it's location, so that I can manually upload it (Don't want to do the uploading through javascript) My question is though, how do I get the clients location of the file from the event, so that I can put it in a <input type="file"> ?

Thanks.

Share Improve this question edited Jul 19, 2012 at 10:47 Anirudh Ramanathan 46.8k23 gold badges135 silver badges194 bronze badges asked Jul 19, 2012 at 10:45 user1020317user1020317 6545 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Here is an example of mozilla using the dataTransfer object of the event to get a list of files dragged

EXAMPLE

My example:

holder.ondrop = function (e) {
    e.preventDefault();
    if(e.dataTransfer && e.dataTransfer.files.length != 0){
        var files = e.dataTransfer.files //Array of filenames
        files[0]; //the first file in the list

        // code to place file name in field goes here...

    }else{
        // browser doesn't support drag and drop.
    }

    console.log(e);
}

use the mozilla example because its more prehensive than I've provided.

The input file field can be treated as a normal input field and its value can be fetched as follows.

<input id="inputid" type="file" dropzone="..."> 

holder.ondrop = function (e) {
    e.preventDefault();
    var path = $('#inputid').val();
    //split the string at the lastIndexOf '/' to get filename 
    console.log(e);
}
发布评论

评论列表(0)

  1. 暂无评论