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

javascript - Get File Path From Drop Event - Stack Overflow

programmeradmin4浏览0评论

I have some javascript code that allows users to drag and drop files in IE.

The problem I have is that they would like the file deleted after being dragged, but I can't seem to get the file path, just the name.

I have seen many references to e.dataTransfer.files[0].path, but it always es back as 'undefined' when I try it.

Any ideas why e.dataTransfer.files[0].path does not work, or how I can get the file path?

 $(document).ready(function (ex) {
        var holder = document.getElementById('holder');
        holder.ondragover = function () { this.className = 'hover'; return false; };
        holder.ondrop = function (e) {
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            var path = e.dataTransfer.files[0].path;
            fileArray.push(file);
            //alert(e.target.id);
            var reader = new FileReader();
            reader.readAsDataURL(file);
        };
    });

I have some javascript code that allows users to drag and drop files in IE.

The problem I have is that they would like the file deleted after being dragged, but I can't seem to get the file path, just the name.

I have seen many references to e.dataTransfer.files[0].path, but it always es back as 'undefined' when I try it.

Any ideas why e.dataTransfer.files[0].path does not work, or how I can get the file path?

 $(document).ready(function (ex) {
        var holder = document.getElementById('holder');
        holder.ondragover = function () { this.className = 'hover'; return false; };
        holder.ondrop = function (e) {
            e.preventDefault();
            var file = e.dataTransfer.files[0];
            var path = e.dataTransfer.files[0].path;
            fileArray.push(file);
            //alert(e.target.id);
            var reader = new FileReader();
            reader.readAsDataURL(file);
        };
    });
Share Improve this question edited Nov 15, 2019 at 15:58 Ru Chern Chong 3,75613 gold badges35 silver badges43 bronze badges asked Nov 15, 2019 at 15:31 J.CartJ.Cart 5722 gold badges9 silver badges25 bronze badges 1
  • For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System – diego orellana Commented Nov 15, 2019 at 15:57
Add a ment  | 

3 Answers 3

Reset to default 10

that's not possible for a web app, but if your users would run an app on their machine, you could build an electron app. inside electron you get the e.dataTransfer.files[0].path property, which you can then use to delete the file.

https://www.electronjs/

The file doesn't have property path.

You can experiment with it here and the plete list of file properties can be found here.

To read the file one uses FileReader as you did, without using the path explicitly.

After trying everything I could find, I am of the opinion that it is not possible.

发布评论

评论列表(0)

  1. 暂无评论