File Dialog From Javascript *without*
Solution 1:
Well, if I understand correct what you want, is some like this...
<inputtype="button" value="Add File" onclick="document.getElementById('file').click()" />
<inputtype="file"id="file" style="display:none" />
Hidding the file
object and calling the file dialog with another object. Right ?
EDIT: Only Javascript
myClickHandler() {
var f = document.createElement('input');
f.style.display='none';
f.type='file';
f.name='file';
document.getElementById('yourformhere').appendChild(f);
f.click();
}
button.onclick = myClickHandler
Put this in your object with the id
of your form
in place of yourformhere
!!
Solution 2:
Here's is a way of doing it without any Javascript and it's also compatible with every browser, including mobile.
BTW, 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
<inputtype="file"style="position: fixed; top: -100em"></label>
If you prefer you can go the "correct way" by using for
in the label
pointing to the id
of the input like this:
<labelfor="inputId">file dialog</label><inputid="inputId"type="file"style="position: fixed; top: -100em">
Solution 3:
I hid my first button like this
<input style="display:none;"type="file"id="file" name="file"/>
The following triggers the input file:
<i class="fa fa-camera-retro fa-3x" type="button" data-toggle="tooltip" title="Escoje tu foto de perfil" id="secondbutton" ></i>
(i used an icon)
Which triggers my second button:
$( "#secondbutton" ).click(function() {
$( "#file" ).click();
});
Solution 4:
Works for me:
exportfunctionpickFile(onFilePicked: (file: File) => void): void {
const inputElemenet = document.createElement('input');
inputElemenet.style.display = 'none';
inputElemenet.type = 'file';
inputElemenet.addEventListener('change', () => {
if (inputElemenet.files) {
onFilePicked(inputElemenet.files[0]);
}
});
constteardown = () => {
document.body.removeEventListener('focus', teardown, true);
setTimeout(() => {
document.body.removeChild(inputElemenet);
}, 1000);
}
document.body.addEventListener('focus', teardown, true);
document.body.appendChild(inputElemenet);
inputElemenet.click();
}
And then:
pickFile((file: File) => {
console.log(file);
})
Post a Comment for "File Dialog From Javascript *without* "