How to open a file / browse dialog using javascript?
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.
Here is a non-jQuery solution. Note you can't just use .click()
as some browsers do not support it.
<script type="text/javascript">
function performClick(elemId) {
var elem = document.getElementById(elemId);
if(elem && document.createEvent) {
var evt = document.createEvent("MouseEvents");
evt.initEvent("click", true, false);
elem.dispatchEvent(evt);
}
}
</script>
<a href="#" onclick="performClick('theFile');">Open file dialog</a>
<input type="file" id="theFile" />
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>