upload a file using javascript code example

Example 1: javascript upload file button

<input id='fileid' type='file' hidden/>
<input id='buttonid' type='button' value='Upload MB' />

Example 2: set file upllaod via javascript to html input

let photo = document.getElementById("image-file").files[0];  // file from input
let req = new XMLHttpRequest();
let formData = new FormData();

formData.append("photo", photo);                                
req.open("POST", '/upload/image');
req.send(formData);

Example 3: javascript upload file button

document.getElementById('buttonid').addEventListener('click', openDialog);

function openDialog() {
  document.getElementById('fileid').click();
}