I create a file upload in laravel in normal action and in normal action file uploaded successfully but when i use ajax to upload file that is not successfully.
I use blade template and this is my form:
{{ Form::open(array('url'=>'send-file','files'=>true, 'method' => 'post')) }}
{{ Form::label('file', 'File' ,array()) }}
{{ Form::file('img', array('id' => 'iefile') ) }}
{{ Form::submit('send') }}
{{ Form::close() }}
<div id="progress">
progress
</div>
And this is my jquery code :
$('form').submit(function(e) { // capture submit
e.preventDefault();
var fd = new FormData(this); // XXX: Neex AJAX2
// You could show a loading image for example...
$.ajax({
url: $(this).attr('action'),
xhr: function() { // custom xhr (is the best)
var xhr = new XMLHttpRequest();
var total = 0;
// Get the total size of files
$.each(document.getElementById('iefile').files, function(i, file) {
total += file.size;
});
// Called when upload progress changes. xhr2
xhr.upload.addEventListener("progress", function(evt) {
// show progress like example
var loaded = (evt.loaded / total).toFixed(2)*100; // percent
$('#progress').text('Uploading... ' + loaded + '%' );
}, false);
return xhr;
},
type: 'post',
processData: false,
contentType: false,
data: fd,
success: function(data) {
// do something...
alert('uploaded');
}
});
});
And this is my Rout in Routs.php
Route::any('send-file', function()
{
$file = Input::file('img');
$destinationPath = dirname(__DIR__).'/uploads';
// If the uploads fail due to file system, you can try doing public_path().'/uploads'
//$filename = str_random(12);
$filename = $file->getClientOriginalName();
$extension =$file->getClientOriginalExtension();
if (Input::hasFile('img'))
{
$upload_success = $file ->move($destinationPath , $filename);
}
echo $destinationPath ;
});
But when i select a file and click on submit button progress section change and display percent of uploaded file. But at end of file upload a error display in chrome console .and this error display in app/storage/logs/laravel.log :
[2015-01-08 05:10:01] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to a member function getClientOriginalName() on a non-object' in /var/www/usb/app/routes.php:28
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []