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?
});
tamir
  • 3,207
  • 2
  • 33
  • 51
  • can you provide your code of what you have tried ? – insomiac Jan 10 '13 at 21:04
  • You should take a look at this SOq. http://stackoverflow.com/questions/8006715/drag-drop-files-into-standard-html-file-input – cggaurav Jan 10 '13 at 21:07
  • @insomiac look at my edit. There's not much code though.. – tamir Jan 10 '13 at 21:11
  • @cggaurav so that means it can't be done? – tamir Jan 10 '13 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? – Vaidas Nov 16 '16 at 14:50

2 Answers2

25

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

$("input[type='file']").prop("files", e.originalEvent.dataTransfer.files);
NassimPHP
  • 1,036
  • 10
  • 11
4

From @NassimPHP 's answer, this worked!

$("input[type='file']").prop("files", e.dataTransfer.files);
jerinisready
  • 936
  • 10
  • 24