15

I have a custom file input:

<div id="wrapper"> <span id="fake-text-input"></span> <button id="select-a-file"></button> <input id="hidden-file-input" type="file" /> </div> 

The input[type="file"] is hidden (display: none) and selecting a file is handled by listening\triggering the click and change events.

I want to support file drop as well. I was able to listen to the drop event when a file is dropped on #fake-text-input but I don't know how to forward the drop event to the input[type="file"].. is it even possible?

I'm not interested in file input opacity tricks :)

$('body').on('drop', '#wrapper', function(e) { var file = e.originalEvent.dataTransfer.files[0]; // I have the file.. now what? }); 
5
  • can you provide your code of what you have tried ? Commented Jan 10, 2013 at 21:04
  • You should take a look at this SOq. stackoverflow.com/questions/8006715/… Commented Jan 10, 2013 at 21:07
  • @insomiac look at my edit. There's not much code though.. Commented Jan 10, 2013 at 21:11
  • @cggaurav so that means it can't be done? Commented Jan 10, 2013 at 22:15
  • I don't know any possibility to append files to the file input. I think it's not allowed for security reasons. But maybe you don't need it. Why can't you just fire POST request using $.ajax({type: "POST", ...}) immediately after you got a file? Commented Nov 16, 2016 at 14:50

2 Answers 2

27

This is worked with me in google chrome , the problem now with other browsers

$("input[type='file']").prop("files", e.originalEvent.dataTransfer.files); 
Sign up to request clarification or add additional context in comments.

2 Comments

well, let me know if you find a solution
@nassimPHP, did you find a solution?
4

From @NassimPHP 's answer, this worked!

$("input[type='file']").prop("files", e.dataTransfer.files); 

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.