doufu1504 2015-01-08 10:21
浏览 35

文件在laravel中上传ajax

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} [] []
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
    • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
    • ¥60 pb数据库修改与连接
    • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
    • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
    • ¥20 神经网络Sequential name=sequential, built=False
    • ¥16 Qphython 用xlrd读取excel报错
    • ¥15 单片机学习顺序问题!!
    • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)