文件在laravel中上传ajax

时间:2015-01-08 10:21:50

标签: php jquery ajax laravel

我在laravel中以正常操作和正常操作文件上传成功上传文件,但是当我使用ajax上传不成功的文件时。

我使用刀片模板,这是我的表格:

{{ 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>

这是我的jquery代码:

 $('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');
          }
        });
    });

这是我在Routs.php中的Rout

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 ;

});

但是当我选择一个文件并点击提交按钮进度部分更改并显示上传文件的百分比时。但在文件上传结束时,Chrome控制台中会显示错误。此错误显示在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} [] []

0 个答案:

没有答案