Using File Picker in HTML Forms
The Filestack File Picker adds a user-friendly interface to HTML forms, allowing users to upload files directly from forms. It supports multiple file types, offers drag-and-drop functionality, and integrates seamlessly with cloud storage services.
Add the web picker to your HTML forms:
- Add a
hidden
input to save the uploaded file URL. - Add a button to open the file picker.
- Add an HTML element for thumbnail preview.
- Add JavaScript code to get the URL of the uploaded file and update the hidden form field. At the same time, display a preview to the user.
- When the form is submitted, a references to your Filestack assets is submitted. You can save it in your database.
Try it yourself:
To test the code, replace the `yourAPIKey` with your actual your API code. Get Your API key Now.
See the Pen Filestack File Picker in HTML Forms by Filestack Marketing (@ffkocvzk-the-encoder) on CodePen.
HTML
Fill the below form
CSS
.field {
padding: 5px;
}
#picker {
width: 240px;
height: 60px;
background-color: orange;
cursor: pointer;
border: none;
color: #fff;
font-weight: 600;
border-radius: 5px;
}
#picker:hover {
background-color: #f1b446;
}
JavaScript
// Set up the picker
const client = filestack.init("yourAPIKey");
const options = {
onUploadDone: updateForm,
maxSize: 10 * 1024 * 1024,
accept: "image/*",
uploadInBackground: false
};
const picker = client.picker(options);
// Get references to the DOM elements
const form = document.getElementById("pick-form");
const fileInput = document.getElementById("fileupload");
const btn = document.getElementById("picker");
const nameBox = document.getElementById("nameBox");
const urlBox = document.getElementById("urlBox");
// Add our event listeners
btn.addEventListener("click", function (e) {
e.preventDefault();
picker.open();
});
form.addEventListener("submit", function (e) {
e.preventDefault();
alert("Submitting: " + fileInput.value);
});
// Helper to overwrite the field input value
function updateForm(result) {
const fileData = result.filesUploaded[0];
fileInput.value = fileData.url;
// If file is resizable image, resize and embed it as a thumbnail preview
if (["jpeg", "png"].indexOf(fileData.mimetype.split("/")[1]) !== -1) {
const container = document.getElementById("thumbnail-container");
const thumbnail = document.getElementById("thumbnail") || new Image();
thumbnail.id = "thumbnail";
thumbnail.src = client.transform(fileData.handle, {
resize: {
width: 200
}
});
if (!container.contains(thumbnail)) {
container.appendChild(thumbnail);
}
}
}
Start building your own file upload solution today.
PREVIEW
Users can preview their uploaded files directly inside the File Picker.
CONNECT TO 20+ CLOUD DRIVES
Connect your app directly to AWS, Facebook, Dropbox, Instagram, and more.
PROGRESS BAR UPLOADS
Give your users confidence that their files are on the way to the right destination.
MULTI-FILE UPLOADS
Let your users upload multiple files at one time for speed and simplicity.
CUSTOM CSS
Filestack blends seamlessly into your website or app. Ask about white-labeling.
CLIENT-SIDE CROP
Users can crop files to perfection before sending them to your website or app.
UNLIMITED UPLOADING
Upload any file type, from any local or cloud source, at any scale.
WEBSITES & HTML
Filestack can even load web content from any public URL and capture the rendered page as an image. Perfect for generating screenshots, previews, or converting HTML-based content into other formats.
SECURE CLOUD STORAGE
Store uploads directly in your cloud storage for ease and accessibility.
"Being smart here, in my opinion, is seriously looking at Filestack to give you a fantastic uploading experience, while you spend your time on your product vision, not already-solved problems."
— CHRIS COYIER, FOUNDER OF CSS-TRICKS & CODEPEN