4

I'd like to make a routine that finds a predetermined amount of files that will always be found in the same location, and will always have the same filenames. Is it possible to find these files and then upload them to the webserver, with minimal user intervention?

Ideally, I'd want the user to click a button only once, which will then find these files locally and then upload them to the webserver. Initially I was going to use a form and make the user click a "Search File" window for each file, but I currently need to upload 20 small files at the same time, so that approach would be quite time consuming.

Any pointers or ideas?

dev404
  • 1,088
  • 13
  • 34
  • 1
    Without elevated privileges (such as a browser plug-in), you can't do this. Normal Javascript cannot get files straight out of the regular user's file system. It can upload files the user selects, but not other files. This is all done for security reasons. Imagine if any web page could upload files from your hard drive without user intervention. – jfriend00 Oct 03 '15 at 06:32
  • I figured it'd be an issue. The files would be uploaded to a local server, it's just that the directory containing these files would contain many more files (all in blocks of 20 files) so that asking the user to select them would be a nightmare when the directory gets really populated eventually. – dev404 Oct 03 '15 at 15:35

2 Answers2

3

Is it possible to find these files and then upload them to the webserver, with minimal user intervention?

User should select user files from user filesysystem at each user action at input type="file" element . User selected files should be accessible at FileList object within onchange event .

If all files are saved at same directory , and browser supports directory attribute , user should be able to upload user selected folder.

Chrome, chromium allow folder upload when webkitdirectory attribute is set ; firefox does not allow folder upload currently, though user can upload multiple files at single file dialog when multiple attribute set set allow_dirs attribute at firefox.

See also

document.querySelector("input").onchange = function() {
  console.log(this.files)
}
<input type="file" directory="directory" allow_dirs="allow_dirs" webkitdirectory="webkitdirectory" />
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • I figured it'd be a security issue after I was done making the post. This is for a server running a local webserver, and I need the user to submit 20 files from a certain directory. The reason why I was wondering if this was possible is cause the directory will have a lot more files in it (generated by another script), so I wasn't sure if it was a good idea to let the user pick the files, as he'd be likely to make mistakes when picking that many files at once. – dev404 Oct 03 '15 at 15:32
0

What you want to do is not directly possible with javascript by design for security purposes. If it was, any website you went to could start pulling files off your machine when you visited.

If you have the possibility of getting the user to run a script on their machine first, you could write a batchfile that would put all the files together into a zip file and then they could add the zip file to be uploaded to your website.

Toby Allen
  • 10,997
  • 11
  • 73
  • 124