Does anyone know of a way to upload a file via Ajax and using drag n' drop from the desktop that supports PlayFramework's ability to convert file uploads to a File object?
I've tried several different methods, and nothing works correctly.
Does anyone know of a way to upload a file via Ajax and using drag n' drop from the desktop that supports PlayFramework's ability to convert file uploads to a File object?
I've tried several different methods, and nothing works correctly.
Not really sure this will qualify as an answer since I'm not a hundred percent sure it will work. But it should work :)
If I understand you correctly, you want to drag files from the desktop and drop them in a drop zone somewhere in your browser. This triggers an ajax upload call to a play server.
I've got the second part of that working, using a straight jquery ajax post. The files are received just fine. For the first part, I'd try using the dnd support in html 5 (scroll down to Dragging Files):
http://www.html5rocks.com/tutorials/dnd/basics/
Here's my successful attempt:
Edit routes file and add
Our controller is
Application
, I'll be using it to keep it simple.Edit your Application controller class
Edit your Application.html in app/views/Application folder/package
Edit your main layout: main.html, located in the app/views folder/package and add this line after jQuery
Final notes Remember to download the script from AJAX Upload Valums, enjoy!
You can also grab the source here.
I tested it in different browsers it works for me at least. Credits to Riyad in Play! mailing list who hinted me about the
request.body
P.S: I'm using the one I posted as a comment before
Edit The answer with code has been added as directed by T.J. Crowder, I agree :)
The simple upload part (not drag&drop just click on "upload a file") is not working with Ie7 & 8 (don't try others ie)
See getting Java Bad File Descriptor Close Bug while reading multipart/form-data http body