I am trying to implement drag n drop in my application, which need the full path of folder being dropped.
I have done something like this
<html>
<head>
<style>
#dropzone {
height:200px;
width: 200px;
border: 10px dashed #0c0;
background-color:cyan;
}
</style>
</head>
<body>
<div id="dropzone" droppable="true" ondrop ="drop(event)" ondragenter="return false" ondragover="return false">
</div>
<script type="text/javascript">
function drop(e) {
e.stopPropagation();
e.preventDefault();
var length = e.dataTransfer.items.length;
for (var i = 0; i < length; i++) {
var entry = e.dataTransfer.items[i].webkitGetAsEntry();
if (entry.isDirectory) {
alert(entry);//here i need to get path of folder being dropped.
}
}
}
</script>
</body>
</html
If I alert e.dataTransfer.files[i].name
inside for loop
then it only show name of folder but not it's path.
Does javascript allow access to local file system? OR any workaround for this.
try
e.dataTransfer.files[0].path
which will give you folder path.
Short anwser: no.
It's easy, all you need to do is get the dropped item as a file.
In your code above you have
e.dataTransfer.items[i].webkitGetAsEntry()
.The webkitGetAsEntry() method is really useful as it returns an ''Entry'' object. You can do cool stuffs like, for instance:
Now, the getAsFile() method returns a File Object which contains all the goodies related to the actual file. Take a look at the below code (I modified your code).
Code :
Or even simplier:
So as you see, nothing keeps you from using both. You get the ''Entry'' object to check if you are dealing with a Folder or a File and you can use the getAsFile() method afterwards to access the information related to the dropped item (like the path).
Hope it helps!