I'm using the Laravel framework. I have a form of adding a new item to the database and in that form the user can also drag and drop a file. Then, a progress bar is displayed until it's completed, using Ajax for uploading the file to the server.
Once submitting that form, I run the addItem
function in a controller and I want to do/check:
- That the file is already hosted in the server (successful upload)
- If the file is hosted in the server, how do I find it? (I gave it a random name)
- If the user chose not to submit the form, I wish to erase that file from the server, so I won't have files that are not connected to any item on my database
Can you suggest any ideas on how to complete these tasks?
To send files by AJAX you need to use
FormData
which is a class ofXMLHttpRequest2
, it doesn't work with IE<10.You also need AJAX2 to show progress.
SAMPLE SUBMIT FORM WITH FILES AND PROGRESS VIA AJAX:
Here I have made an example. In this example the form sends the data and files via AJAX using
FormData
and show the upload progress percentage in#progress
using theprogress
event. Obviously it is a sample and it could be changed to adapt it.See how works!!: http://jsfiddle.net/0xnqy7du/3/
LARAVEL:
In
laravel
you can get the file withInput::file
, move to another location and save in the database if you need it: