Is there any way to open the browse for files dialog box when a <a href> link is clicked using javascript? It should function like a normal browse for files button and give the names/list of files selected in response.
-
You could also try this: code.google.com/p/upload-at-clickJohn– John2011-06-24 04:46:29 +00:00Commented Jun 24, 2011 at 4:46
-
3functions are not necessary to restrict file extensions <input id="foto" name="foto" type="file" accept="image/jpeg, image/jpg"/>user1823885– user18238852012-11-14 13:46:56 +00:00Commented Nov 14, 2012 at 13:46
10 Answers
Here is a non-jQuery solution.
(Note you can't just use .click() as most modern browsers do not support it.)
function performClick(elemOrSelector) {
const elem = elemOrSelector instanceof Element ? elemOrSelector : document.querySelector(elemOrSelector);
if (elem && document.createEvent) {
const evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
}
document.querySelector('a').addEventListener('click', ev => {
performClick('#theFile');
});
<a href="#">Link to Trigger Click on File Input Below</a>
<br><br><input type="file" id="theFile" />
4 Comments
.click() all modern browsers (including ie10) support .click() method jsfiddle.net/vnYVB . Don't use display:none on input type:file because this won't work on safari. To fix this safari problem you should use position:fixed;top:-1000 to hide the input type:file. (I know this is old but this post is ranking high at search engines and may mislead passer bys)elem.dispatchEvent(...) instead of node.dispatchEvent(...)Use this.
<script>
function openFileOption()
{
document.getElementById("file1").click();
}
</script>
<input type="file" id="file1" style="display:none">
<a href="#" onclick="openFileOption();return;">open File Dialog</a>
1 Comment
display: none seems to not work on Safari. Maybe you should put it into a div with overflow: hidden; and set position: relative; top: -1000px on the input ?Create input element.
Missing from these answers is how to get a file dialog without a input element on the page.
The function to show the input file dialog.
function openFileDialog (accept, callback) { // this function must be called from a user
// activation event (ie an onclick event)
// Create an input element
var inputElement = document.createElement("input");
// Set its type to file
inputElement.type = "file";
// Set accept to the file types you want the user to select.
// Include both the file extension and the mime type
inputElement.accept = accept;
// set onchange event to call callback when user has selected file
inputElement.addEventListener("change", callback)
// dispatch a click event to open the file dialog
inputElement.dispatchEvent(new MouseEvent("click"));
}
NOTE the function must be part of a user activation such as a click event. Attempting to open the file dialog without user activation will fail.
NOTE
input.acceptis not used in Edge
Example.
Calling above function when user clicks an anchor element.
// wait for window to load
window.addEventListener("load", windowLoad);
// open a dialog function
function openFileDialog (accept, multy = false, callback) {
var inputElement = document.createElement("input");
inputElement.type = "file";
inputElement.accept = accept; // Note Edge does not support this attribute
if (multy) {
inputElement.multiple = multy;
}
if (typeof callback === "function") {
inputElement.addEventListener("change", callback);
}
inputElement.dispatchEvent(new MouseEvent("click"));
}
// onload event
function windowLoad () {
// add user click event to userbutton
userButton.addEventListener("click", openDialogClick);
}
// userButton click event
function openDialogClick () {
// open file dialog for text files
openFileDialog(".txt,text/plain", true, fileDialogChanged);
}
// file dialog onchange event handler
function fileDialogChanged (event) {
[...this.files].forEach(file => {
var div = document.createElement("div");
div.className = "fileList common";
div.textContent = file.name;
userSelectedFiles.appendChild(div);
});
}
.common {
font-family: sans-serif;
padding: 2px;
margin : 2px;
border-radius: 4px;
}
.fileList {
background: #229;
color: white;
}
#userButton {
background: #999;
color: #000;
width: 8em;
text-align: center;
cursor: pointer;
}
#userButton:hover {
background : #4A4;
color : white;
}
<a id = "userButton" class = "common" title = "Click to open file selection dialog">Open file dialog</a>
<div id = "userSelectedFiles" class = "common"></div>
Warning the above snippet is written in ES6.
2 Comments
window.browseForFile() api yet?Unfortunately, there isn't a good way to browse for files with a JavaScript API. Fortunately, it's easy to create a file input in JavaScript, bind an event handler to its change event, and simulate a user clicking on it. We can do this without modifications to the page itself:
$('<input type="file" multiple>').on('change', function () {
console.log(this.files);
}).click();
this.files on the second line is an array that contains filename, timestamps, size, and type.
Comments
Here's is a way of doing it without any Javascript and it's also compatible with any browser.
EDIT: In Safari, the input gets disabled when hidden with display: none. A better approach would be to use position: fixed; top: -100em.
<label>
Open file dialog
<input type="file" style="position: fixed; top: -100em">
</label>
Also, if you prefer you can go the "correct way" by using for in the label pointing to the id of the input like this:
<label for="inputId">file dialog</label>
<input id="inputId" type="file" style="position: fixed; top: -100em">
1 Comment
position: fixed; top: -100em; or margin-top: -2em and having overflow: hidden in the label.you can't use input.click() directly, but you can call this in other element click event.
html
<input type="file">
<button>Select file</button>
js
var botton = document.querySelector('button');
var input = document.querySelector('input');
botton.addEventListener('click', function (e) {
input.click();
});
this tell you Using hidden file input elements using the click() method
Comments
How about make clicking the a tag, to click on the file button?
There is more browser support for this, but I use ES6, so if you really want to make it work in older and any browser, try to transpile it using babel, or just simply use ES5:
const aTag = document.getElementById("open-file-uploader");
const fileInput = document.getElementById("input-button");
aTag.addEventListener("click", () => fileInput.click());
#input-button {
position: abosulte;
width: 1px;
height: 1px;
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
}
<a href="#" id="open-file-uploader">Open file uploader</a>
<input type="file" id="input-button" />
Comments
These days you can also use the File System API
https://developer.mozilla.org/en-US/docs/Web/API/File_System_API
It allows for file browsing, and can go deeper to offer functions like saving of files too. Here is a demo, although it requires a secure context and can't be used in a cross origin iframe:
<a href="#">Pick a file</a>
<p>File name: <span id="file-name">None</span></p>
<script>
async function getFile() {
// Open file picker and destructure the result the first handle
const [fileHandle] = await window.showOpenFilePicker();
const file = await fileHandle.getFile();
document.getElementById("file-name").textContent = file.name;
return file;
}
document.querySelector("a").addEventListener("click", () => getFile());
</script>
Comments
I know this is an old post, but another simple option is using the INPUT TYPE="FILE" tag according to compatibility most major browser support this feature.