$(\'#id\').click();
It doesn\'t work on Chrome 26 on Mac OS.
The problem actually is creation "upload" widget that can be integrated in
READY TO USE FUNCTION (using Promise)
/**
* Select file(s).
* @param {String} contentType The content type of files you wish to select. For instance "image/*" to select all kinds of images.
* @param {Boolean} multiple Indicates if the user can select multiples file.
* @returns {Promise} A promise of a file or array of files in case the multiple parameter is true.
*/
function (contentType, multiple){
return new Promise(resolve => {
let input = document.createElement('input');
input.type = 'file';
input.multiple = multiple;
input.accept = contentType;
input.onchange = _ => {
let files = Array.from(input.files);
if (multiple)
resolve(files);
else
resolve(files[0]);
};
input.click();
});
}
TEST IT
// Content wrapper element
let contentElement = document.getElementById("content");
// Button callback
async function onButtonClicked(){
let files = await selectFile("image/*", true);
contentElement.innerHTML = files.map(file => ``).join('');
}
// ---- function definition ----
function selectFile (contentType, multiple){
return new Promise(resolve => {
let input = document.createElement('input');
input.type = 'file';
input.multiple = multiple;
input.accept = contentType;
input.onchange = _ => {
let files = Array.from(input.files);
if (multiple)
resolve(files);
else
resolve(files[0]);
};
input.click();
});
}