2015-02-17 6 views
1

Я пытаюсь загрузить файл через ajax в Laravel.Загрузка файла через Ajax в Laravel

$("#stepbutton2").click(function(){ 
      var uploadFile = document.getElementById("largeImage"); 
      if(""==uploadFile.value){ 


      } 
      else{ 
       var fd = new FormData(); 

       fd.append("fileInput", $("#largeImage")[0].files[0]); 

       $.ajax({ 
        url: '/nominations/upload/image', 
        data: fd, 
        processData: false, 
        contentType: false, 
        type: 'POST', 
        success: function(data){ 
         if(data.uploaded==true){ 
          alert(data.url); 
         } 
        }, 
        error: function(err){ 
         alert(err); 
        } 
       }); 

      } 
     }); 

Я передаю входной файл в скрипт php.

public function image(){ 

$file = Input::file('fileInput'); 
    $ext = $file->getClientOriginalExtension(); 
    $fileName = md5(time()).".$ext"; 

    $destinationPath = "uploads/".date('Y').'/'.date('m').'/'; 
    $file->move($destinationPath, $fileName); 
    $path = $file->getRealPath(); 
    return Response::json(["success"=>true,"uploaded"=>true, "url"=>$path]); 


    } 

Я получаю ответ, как

{"success":true,"uploaded":true,"url":false} 

Запрос Payload является

------WebKitFormBoundary30GMDJXOsygjL0ZS 
Content-Disposition: form-data; name="fileInput"; filename="DSC06065 copy.jpg" 
Content-Type: image/jpeg 

Почему это происходит?

+0

Попробуйте получить путь перед перемещением файла – Musa

+0

Ответ «{« success »: true,« uploaded »: true,« url »:« C: \\ wamp \\ tmp \\ php7A29.tmp »}' – user1012181

+0

И ... это не то, что вы хотите? – Musa

ответ

1

Найдено ответ:

public function image(){ 

     $file = Input::file('fileInput'); 
      $ext = $file->getClientOriginalExtension(); 
      $fileName = md5(time()).".$ext"; 

      $destinationPath = "uploads/".date('Y').'/'.date('m').'/'; 
      $moved_file = $file->move($destinationPath, $fileName); 
      $path = $moved_file->getRealPath(); 
      return Response::json(["success"=>true,"uploaded"=>true, "url"=>$path]); 


      } 

Получить путь после назначения его новой переменной.