Home » Jquery » javascript – Open file in new browser tab by input file value-Exceptionshub

javascript – Open file in new browser tab by input file value-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I want to open the file in a new browser tab for previewing.

<input id="myinput" type="file" onchange="oninputfileselected(this);"/>   
function oninputfileselected(obj) {
  var inputfile = document.getElementById($(obj).attr('id'));
  console.log(inputfile.files);
}

The console output I get doesn’t contain any local path for me to open a new tab. I am not sure if there is another way to do this? Thank you

How to&Answer:

The only approach I can think of would be to use FileReader to read the data in the file, then generate a data: scheme URL from it, then open that URL with window.open.