- HTML5: File Input with Folder Selection
- How to access the selected Folders via JavaScript
- Browser Support
- About the Author
- Related Topics
- 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
- jQuery: Send HTML5 Canvas to Server via Ajax
- HTML5: Upload Images with Client Side Resize
- Important Note
- Participate
- Uploading files made easy with the .selectFile command
- Selecting files in an HTML5 input element
- Dragging files into the browser
- Manipulating binary data with Buffer
- Migrating from the cypress-file-upload plugin
- Resources
- We’d like to hear from you!
- Uploading files made easy with the .selectFile command
- Selecting files in an HTML5 input element
- Dragging files into the browser
- Manipulating binary data with Buffer
- Migrating from the cypress-file-upload plugin
- Resources
- We’d like to hear from you!
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
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
Related Topics
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
jQuery: Send HTML5 Canvas to Server via Ajax
HTML5: Upload Images with Client Side Resize
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.
Uploading files made easy with the .selectFile command
Today we’re excited to announce a built-in way for you to select files in an HTML5 input element and simulate dragging files into the browser with the introduction of the .selectFile() command, new in Cypress 9.3.0.
Selecting files in an HTML5 input element
With the new .selectFile() command, you can easily select a fixture file in a form element:
cy.get('input[type=file]').selectFile('file.json')
Or multiple fixture files, as long as the file input has the multiple property:
cy.get('input[type=file]').selectFile(['file.json', 'file2.json'])
You can also select a file created dynamically inside of a test, using the new Cypress.Buffer utility, also added in 9.3.0:
cy.get('input[type=file]').selectFile(< contents: Cypress.Buffer.from('file contents'), fileName: 'file.txt', lastModified: Date.now(), >)
Dragging files into the browser
The new .selectFile() command also allows you to simulate dragging and dropping a file over an element, using the drag-drop action:
cy.get('input[type=file]').selectFile('file.json', < action: 'drag-drop' >)
And you can even drop a file over the document:
cy.document().selectFile('file.json', < action: 'drag-drop' >)
Manipulating binary data with Buffer
Cypress now exposes an API for manipulating binary data, similar to the nodejs Buffer class, as Cypress.Buffer .
Not only are instances of Cypress.Buffer accepted by .selectFile() to specify files inline, but the cy.readFile() and cy.fixture() commands yield instances of Cypress.Buffer when the encoding is set to null :
cy.readFile('images/logo.png', null).then((file) => < // file will be read as a buffer // and should look something like this: // Buffer([0, 0, . ]) expect(Cypress.Buffer.isBuffer(file)).to.be.true >) cy.fixture('images/logo.png', null).then((logo) => < // logo will be read as a buffer // and should look something like this: // Buffer([0, 0, . ]) expect(Cypress.Buffer.isBuffer(logo)).to.be.true >)
Migrating from the cypress-file-upload plugin
The .selectFile() command replaces the cypress-file-upload community plugin by Paul Auramenka, and moving forward we recommend that you update your tests to use the new command.
In order to streamline this process, a comprehensive Migration guide has been written which explains the exact steps you need to take to update existing tests to use .selectFile() .
Resources
We’d like to hear from you!
The Cypress team has been working hard to deliver this improved experience. We’re excited to bring new APIs like .selectFile() and Cypress.Buffer to our users, and as always, we’re eager to hear your feedback.
You can submit an issue on Github or chat with us on our Discord. Thanks for your support, and happy testing!
Uploading files made easy with the .selectFile command
Today we’re excited to announce a built-in way for you to select files in an HTML5 input element and simulate dragging files into the browser with the introduction of the .selectFile() command, new in Cypress 9.3.0.
Selecting files in an HTML5 input element
With the new .selectFile() command, you can easily select a fixture file in a form element:
cy.get('input[type=file]').selectFile('file.json')
Or multiple fixture files, as long as the file input has the multiple property:
cy.get('input[type=file]').selectFile(['file.json', 'file2.json'])
You can also select a file created dynamically inside of a test, using the new Cypress.Buffer utility, also added in 9.3.0:
cy.get('input[type=file]').selectFile(< contents: Cypress.Buffer.from('file contents'), fileName: 'file.txt', lastModified: Date.now(), >)
Dragging files into the browser
The new .selectFile() command also allows you to simulate dragging and dropping a file over an element, using the drag-drop action:
cy.get('input[type=file]').selectFile('file.json', < action: 'drag-drop' >)
And you can even drop a file over the document:
cy.document().selectFile('file.json', < action: 'drag-drop' >)
Manipulating binary data with Buffer
Cypress now exposes an API for manipulating binary data, similar to the nodejs Buffer class, as Cypress.Buffer .
Not only are instances of Cypress.Buffer accepted by .selectFile() to specify files inline, but the cy.readFile() and cy.fixture() commands yield instances of Cypress.Buffer when the encoding is set to null :
cy.readFile('images/logo.png', null).then((file) => < // file will be read as a buffer // and should look something like this: // Buffer([0, 0, . ]) expect(Cypress.Buffer.isBuffer(file)).to.be.true >) cy.fixture('images/logo.png', null).then((logo) => < // logo will be read as a buffer // and should look something like this: // Buffer([0, 0, . ]) expect(Cypress.Buffer.isBuffer(logo)).to.be.true >)
Migrating from the cypress-file-upload plugin
The .selectFile() command replaces the cypress-file-upload community plugin by Paul Auramenka, and moving forward we recommend that you update your tests to use the new command.
In order to streamline this process, a comprehensive Migration guide has been written which explains the exact steps you need to take to update existing tests to use .selectFile() .
Resources
We’d like to hear from you!
The Cypress team has been working hard to deliver this improved experience. We’re excited to bring new APIs like .selectFile() and Cypress.Buffer to our users, and as always, we’re eager to hear your feedback.
You can submit an issue on Github or chat with us on our Discord. Thanks for your support, and happy testing!