Browse Folders

Contact US

*Tek-Tips’s functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Javascript FAQ

Custom User Controls

Code to call the folder dialog box

CODE

CODE

var currentFolder=»»;
function GetDriveList() var fso, obj, n, e, item, arr=[];
try <
fso = new ActiveXObject(«Scripting.FileSystemObject»);
>
catch(er) alert(‘Could not load Drives. The ActiveX control could not be started.’);
cancelFolder();
>

var subfolders=GetSubFolderList(fld);
for(var j=0;j ‘+subfolders[j]+»

«;
>
divList.innerHTML=list;
resizeList();
divPath.scrollIntoView();
>
function resizeList() var divList=document.getElementById(«list»);
var divPath=document.getElementById(«path»);
if(document.body.clientHeight>0 && divPath.offsetHeight>0) divList.style.height=document.body.clientHeight-divPath.scrollHeight;
>
>
function highlight(div) div.className=»folderButton»;
>
function unhighlight(div) div.className=»folders»;
>
function selectFolder() window.returnValue=currentFolder;
window.close();
>
function cancelFolder() window.returnValue=»»;
window.close();
>
—>


You must allow the ActiveX control to run in order to use this dialog.


My Archive

Copyright © 1998-2023 engineering.com, Inc. All rights reserved.
Unauthorized reproduction or linking forbidden without expressed written permission. Registration on or use of this site constitutes acceptance of our Privacy Policy.

Источник

HTML5: File Input with Folder Selection

First, we have a look at the required HTML. We are using a normal input field with the type «file». In order to be able to select multiple files at once, we have added the attribute «multiple». Additionally, we have added an onchange event to read and process the folder respectively the files from the selected folder with JavaScript. We will look at the JavaScript code in the next section.

For making a directory input out of the usual file input, additionally, we have specified the attributes webkitdirectory, mozdirectory, msdirectory, odirectory and directory. Actually, specifying only the attribute «directory» should be enough at that point, but because we also want to support some older browsers, we have also added the browser specific attributes with the prefix webkit-, moz-, ms- and o-. If the browser is supporting one of those attributes, instead of the file dialog, now the directory dialog of the system is displayed.

How to access the selected Folders via JavaScript

Last, I also want to show you, how to work with the selected folder and how you are able to access the files of the folder which the user has selected.

For this, we are using the following JavaScript, which will be automatically executed when changing the folder selection, because we have assigned the function to the onchange event of the input field.

We get all files located in the selected folder in the array e.target.files.

In the code above, we are just looping through this array (the number of files is e.target.files.length) in order to show some information about the files such as name, size and type using an alert.

Browser Support

This input type is supported by the browsers Google Chrome (from version 30), Microsoft Edge (from version 14), Mozilla Firefox (from version 50) and Opera (from version 17). In contrast, the selection of folders is not supported by the Internet Explorer and many mobile browsers. This is also due to the fact that many mobile systems do not allow folders to be selected in their system at all.

About the Author

Avatar

You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? — sttmedia.com/contact
Show Profile

Rename File to its Folder Name

How to resize Image before Upload in Browser

HTML5 Canvas: Beginner Tutorial Chapter 1 — Introduction and Basics

HTML: Preassign HTML Form with Data

Send HTML5 Canvas as Image to Server

HTML5: Upload Images with Client Side Resize

jQuery: Send HTML5 Canvas to Server via Ajax

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.

Источник

How to get folder directory from html input type «file» or any other way?

The HTML input type «file» allows users to select a file from their local device to upload to a web server. However, it does not provide a way to directly access the directory of the selected file. In some cases, it may be necessary to get the folder directory of the selected file, either for display purposes or to use as a part of a file path. Here are a few methods that can be used to get the folder directory from an HTML input type «file».

Method 1: Using JavaScript

To get the folder directory from an HTML input type «file» using JavaScript, you can use the following steps:

  1. Create an HTML input element with type «file» and an onchange event listener to trigger a function when a file is selected.
input type="file" onchange="handleFiles(this.files)">
  1. In the function triggered by the onchange event, access the file object and extract the file path using the webkitRelativePath property.
function handleFiles(files)  const filePath = files[0].webkitRelativePath; console.log(filePath); >
  1. The file path extracted from the webkitRelativePath property will include the file name. To remove the file name and get only the folder directory, you can use the substring() method.
function handleFiles(files)  const filePath = files[0].webkitRelativePath; const folderPath = filePath.substring(0, filePath.lastIndexOf('/')); console.log(folderPath); >
  1. The folder path extracted using the substring() method will include the file system root, which may vary depending on the user’s operating system. To remove the file system root and get only the relative path from the file input element, you can use the replace() method.
function handleFiles(files)  const filePath = files[0].webkitRelativePath; const folderPath = filePath.substring(0, filePath.lastIndexOf('/')).replace(/^.*[\\\/]/, ''); console.log(folderPath); >

This code will log the folder directory to the console when a file is selected in the file input element. Note that this method may not work in all browsers and operating systems, as it relies on the webkitRelativePath property which is only supported in some browsers.

Method 2: Using a File Path Library

To get the folder directory from an HTML input type «file» using a file path library, you can use the following steps:

  1. First, you need to include the file path library in your HTML file. You can use the following code to include the library:
script src="path/to/file/path/library.js"> script>
  1. Next, you need to create an input element with the type «file» in your HTML file. You can use the following code to create the input element:
input type="file" id="fileInput">
  1. Now, you need to add an event listener to the input element to detect when a file is selected. You can use the following code to add the event listener:
document.getElementById("fileInput").addEventListener("change", function()  var filePath = document.getElementById("fileInput").value; var folderPath = path.dirname(filePath); console.log(folderPath); >);
  1. Finally, when a file is selected, the event listener will get the file path and use the file path library to get the folder directory. The folder directory will then be logged to the console.
DOCTYPE html> html> head> title>Get Folder Directory from File Inputtitle> script src="path/to/file/path/library.js"> script> head> body> input type="file" id="fileInput"> script> document.getElementById("fileInput").addEventListener("change", function()  var filePath = document.getElementById("fileInput").value; var folderPath = path.dirname(filePath); console.log(folderPath); >); script> body> html>

This code will allow you to get the folder directory from an HTML input type «file» using a file path library.

Method 3: Server-side Processing

To get the folder directory from HTML input type «file» using server-side processing, you can use the following steps:

form action="upload.php" method="post" enctype="multipart/form-data"> input type="file" name="fileToUpload" id="fileToUpload"> input type="submit" value="Upload File" name="submit"> form>
  1. In the server-side script (in this example, we’re using PHP), check if a file was uploaded and get the directory path using the realpath() function.
if(isset($_POST["submit"]))  $target_dir = realpath($_FILES["fileToUpload"]["name"]); echo "Directory path: " . $target_dir; >
  1. If you want to get only the directory path without the file name, you can use the dirname() function.
if(isset($_POST["submit"]))  $target_dir = realpath($_FILES["fileToUpload"]["name"]); $directory_path = dirname($target_dir); echo "Directory path: " . $directory_path; >
  1. To handle errors, you can check if the file was uploaded successfully and if the directory path exists.
if(isset($_POST["submit"]))  $target_dir = realpath($_FILES["fileToUpload"]["name"]); if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir))  if(file_exists($target_dir))  $directory_path = dirname($target_dir); echo "Directory path: " . $directory_path; > else  echo "Error: Directory path does not exist."; > > else  echo "Error: File upload failed."; > >

That’s it! With these steps, you can get the folder directory from HTML input type «file» using server-side processing in PHP.

Источник

HTML file inputs support picking directories

HTML’s accepts a single file. You can allow multiple files via .
But sometimes, selecting multiple files is not enough and can be cumbersome for the user. Enter webkitdirectory :

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies

Using webkitdirectory switches the browser’s file picker to select a directory. All files inside that directory, and inside any nested subdirectories, will be selected for the file input.

This can be useful when users want to upload all files from a nested directory structure. Note that users could also add lots of files if they selected a root directory or similar.

Usage

Add webkitdirectory to your file input and you are good to go. Your event handler needs to handle event.target.files just like for a multiple input.

Note that file entries will have a webkitRelativePath property which lists the relative file path, starting from the selected directory (including it).

Note that Chrome will show a confirmation prompt to make users aware that they selected a directory. Firefox and Edge won’t.

Browser support

Although the property is called webkitdirectory and not directory , it is supported by Chrome, Firefox, Edge 14+, and Safari 11.1+.
Older browsers support multiple , so you should offer that as a fallback.

I’ve seen examples using directory but there are no indications this attribute will become standard soon. Feel free to use to feel future-proof.

On JavaScript file objects, webkitRelativePath is supported similarly to webkitdirectory in HTML. As a fallback for older browsers you can use name (which won’t include any path).

Источник

Читайте также:  Java database driver mysql
Оцените статью