2017-02-17 12 views
0

私はデータベースとフォルダにajaxとlaravelを使用して画像をアップロードしようとしていますが、call to a member function getclientoriginalextension on stringのようなエラーが発生しています。 。ajax laravelを使用してデータベースとフォルダに画像をアップロード5.4

私の見解:

<form role="form" name="campaignForm" id="campaignForm" action="" method="post" enctype="multipart/form-data"> 


    <input type="text" name="project_name" autocomplete="off" id="project_name" placeholder="Company name" class="form-control"> 

    <input type="text" name="website_url" id="website_url" autocomplete="off" placeholder="http://www.yourdomain.com" class="form-control"> 
    <input type="file" name="image" id="image" autocomplete="off" placeholder="" class="form-control"> 

    <input type="text" name="location" id="location" autocomplete="off" placeholder="Enter the location you want to target" class="form-control"> 

    <input type="text" name="group" id="group" autocomplete="off" placeholder="Group (optional)" class="form-control"> 
    <button type="submit" id="btn-save" value="add" class="btn actionBtn btn-primary"> 
</form> 

私のAjax:

$("#btn-save").click(function (e) { 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') 
     } 
    }) 

    e.preventDefault(); 

    var formData = { 
     project_name: $('#project_name').val(), 
     website_url: $('#website_url').val(), 
     location: $('#location').val(), 
     group: $('#group').val(), 

     image : $('#image').val(), 
    } 


    $.ajax({ 

     type: type, 
     url: my_url, 
     data: formData, 
     dataType: 'json', 
     success: function (data) { 
      console.log(data); 

     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 

コントローラ:

public function campaign(Request $request){ 


    $task = new projects(); 

    $task->project_name = trim($request->project_name); 
    $task->website_url = trim($request->website_url); 
    $task->location = trim($request->location); 
    $task->group = trim($request->group); 


    $file = trim($request->image); 

    if ($request->hasFile($file)) { 
    $destinationPath = 'images'; // upload path 
    $extension = $file->getClientOriginalExtension(); // getting image extension 
    $fileName = rand(11111,99999).'.'.$extension; // renameing image 
    $file->move($destinationPath, $fileName); 

    $task->image = $fileName; 
    } 

    dd($task); 

} 

任意のヘルプは.Thankあなたいただければ幸いです。

答えて

1

Laravelがアップロードされた画像を受け取らないように見えます。 $request->imageUploadedFileクラスのインスタンスでなければなりませんが、あなたの場合には、それはthis answerに敬意を与える"C:\fakepath\custom-image.jpg"

のような単なる文字列で、これを試してみてください。

$("#btn-save").click(function (e) { 
    $.ajaxSetup({ 
     headers: { 
      'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') 
     } 
    }); 

    e.preventDefault(); 

    var form = document.forms.namedItem("campaignForm"); // high importance!, here you need change "yourformname" with the name of your form 
    var formData = new FormData(form); // high importance! 

    $.ajax({ 
     type: type, 
     url: my_url, 
     dataType: "json", // or html if you want... 
     contentType: false, // high importance! 
     data: formData, // high importance! 
     processData: false, // high importance! 
     success: function (data) { 
      console.log(data); 

     }, 
     error: function (data) { 
      console.log('Error:', data); 
     } 
    }); 
}); 
+0

そのiが編集しようとしていたときに、データ.Butを挿入するために働いていました空の値として格納します。なぜ私は理解できないのですか? – 06011991

+0

それはうまく働いた。既存のインスタンスを編集することを意味しますか? 'フォームのフィールドにold( 'username' 。ここに[古い入力文書](https://laravel.com/docs/5.4/requests#old-input) –

+0

私は問題を整理しました。すべてが魅力のように働いています。ありがとうございました:) – 06011991

関連する問題