0

You know how a file selection browser popup opens when you click on an HTML input element with type file? Can I trigger that popup to appear in javascript?

At a certain point in my javascript, I'd like for the popup to appear so that user can upload a text file. Then, I plan to turn the file blob data into an arraybuffer with the HTML5 FileReader API.

I've tried triggering the popup by faking a click to a file input element to no avail:

// Does not work.
var input = document.createElement('input');
input.type = 'file';
input.click();

How can I write a javascript function to trigger the file selection popup? I know that I can instead rely on an HTML5 drag and drop interface for uploading files, but I prefer the popup.

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206
  • 1
    As far as I know, It's not possible due to a security reason. Or some browser allow it, but browser compatibility isn't guaranteed. You might want to take a look at this one. http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input – hina10531 Dec 17 '14 at 06:05
  • 1
    A possible answer for you here by the way. http://stackoverflow.com/a/15876384/1664330 – hina10531 Dec 17 '14 at 06:12

1 Answers1

0

The element is created but not added to the DOM.

You should add it to another element

JNF
  • 3,696
  • 3
  • 31
  • 64