Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload folder and all its content in JavaScript

Tags:

javascript

How do I upload the content of a folder using JavaScript (client side)? The FileSystem API has not been adopted by browsers other than Chrome; I only get a File item with the name of the folder.

It should be possible, because Google Drive allows to drop a folder and all the content (folders and files) will be uploaded automatically.

like image 268
Eduardo Alonso Avatar asked Feb 15 '17 02:02

Eduardo Alonso


People also ask

How get all file of a folder by JavaScript?

js fs core module to get all files in the directory, we can use following fsmethods. fs. readdir (path, callbackFunction) — This method will read all files in the directory. You need to pass directory path as the first argument and in the second argument, you can any callback function.

How can I upload an entire folder?

Upload files & folders On your computer, go to drive.google.com. File Upload or Folder Upload. Choose the file or folder you want to upload.


1 Answers

You can actually upload directories in all latest versions of Chrome, Firefox and Microsoft Edge. There are numerous working examples available to look at.

Here is a good, working example that I've previously used in a project

Quarklemotion Html5FileSelector

In addition, Dropzone JS also supports directory uploads as well and it works in Chrome, FF and Edge. I've just transitioned to using this in my own project.

Dropzone JS

These solutions recursively read the directory entries and list all of the files including their relative paths. If you want to rebuild the folder structure when uploading you will have to implement that using the relative paths and the appropriate algorithm.

like image 181
CaseyC Avatar answered Sep 22 '22 21:09

CaseyC