0

I am uploading an image from my PC. It is working fine in Firefox, but in Chrome, the dialog to select the file doesn't open up! I'm invoking click event of input type in Javascript.

Here is what I'm doing:

<input type="file" id="fileElem" accept="image/*" style="display:none" > <div id="fileSelect" class="drop-area">Select some files</div> 

Here is Javascript:

var fileSelect = document.getElementById("fileSelect"), fileElem = document.getElementById("fileElem"); fileElem.addEventListener("click",function(e){ var files = this.files handleFiles(files) },false) fileSelect.addEventListener("click", function (e) { fileElem.click(); e.preventDefault(); }, false); 
4
  • No. I don't have form element. I'm just using the html as i have shown! Commented Jul 18, 2012 at 9:48
  • It's Working on Google Chrome 20.0.1132.57 m Commented Jul 18, 2012 at 9:52
  • Which version of chrome you are using? Commented Jul 18, 2012 at 9:52
  • I'm using chromium on Ubuntu! Commented Jul 18, 2012 at 9:54

1 Answer 1

2

No browser that I know of lets you simulate click on an <input type="file"> itself without user intervention. The reason is security. Browsers require that a user make an explicit manual click (user-initiated click) somewhere on the page. However, once that happens, it’s straightforward to hijack the click and route it to a file input. This is what you're trying to do.

See my blob post on the topic. It has a demo that should work: http://ericbidelman.tumblr.com/post/14636214755/making-file-inputs-a-pleasure-to-look-at

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.