Dragging & dropping files

Your application can support drag-and-drop, allowing users to drop files directly onto the app instead of selecting them through a file picker.

When files are dragged over the application, the Drag Over state becomes yes, which can be used to update your UI (for example, highlighting a drop zone).

If the user drops the files onto the window, the File System - File(s) Dropped event will trigger. The file locations can then be accessed through the Paths Of Dropped File(s) state.


You can also support dragging files out of your app into other applications using the Make Draggable Element.

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2FKeFy7xu4jXPtMmvk1x60%2FdragAndDropExample2.gif?alt=media&token=211a262b-c3ee-46bb-bd4c-9ff8bace9f68" alt=""><figcaption></figcaption></figure>


How To:

To make our application able to accept incoming file drops, we need to set the Enable File Dropping property to "yes" in our Filesystem Element.

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2FcWfk0dHcebbwVudomKJm%2Fimage.png?alt=media&token=28538ccd-5e42-46b8-b981-dc4ec9e5fad0" alt=""><figcaption></figcaption></figure>

Step 1.

Firstly, we want to detect when a user is dragging a file over our app, so we can update our UI accordingly.

This is simple to do, we can just use a conditional based on the Drag Over state.

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2FCRZzwXr4Ot60tOPFyIGO%2Fimage.png?alt=media&token=05ba6a9e-63e7-4a1c-baad-dfb3d5c8dfeb" alt=""><figcaption></figcaption></figure>

Step 2.

Next, we'll handle the actual file dropping event. We'll listen for the File System - File(s) Dropped event.

And then we'll set the value of our file uploader, to automatically upload the dropped file using the Paths Of Dropped File(s) state.

For more details on how the file uploader works, check the handling file uploads guide.

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2F8jc3sU5rKfR22DrlwZIH%2Fimage.png?alt=media&token=6f723c19-3779-4df9-a9a9-493f87b30f8e" alt=""><figcaption></figcaption></figure>

Step 3.

Next step is to make a draggable element that we can drag outside of our app, onto other applications.

For this, we'll add an ID Attribute to the element we want to become "droppable"

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2FLdIbMU0YO3DONoREIXbo%2Fimage.png?alt=media&token=190d948d-297e-45be-b308-43f15d691958" alt=""><figcaption></figcaption></figure>

Step 4.

Lastly, add the Make Draggable Element to your page, and target our specified ID Attribute.

<figure><img src="https://909455594-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FC8kwY8yODXmNxAtaN0Ej%2Fuploads%2Fq3qXoaZL8GZEjqfGY3la%2Fimage.png?alt=media&token=444ae0ab-6b8a-4e7f-89a9-30a6b2c39f45" alt=""><figcaption></figcaption></figure>

For tips on getting file paths, read the navigating directories guide.