2017-02-17 81 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); 

} 

任何帮助,将不胜感激。谢谢您。

回答

1

看起来像Laravel没有收到上传的图像。 $request->image应该UploadedFile类的实例,但在你的情况下,它就像"C:\fakepath\custom-image.jpg"

给予对于this answer只是字符串,试试这个:

$("#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

它的工作将数据插入。但当我试图编辑形式,其存储为空值。我不明白为什么? – 06011991

+0

我很高兴它的工作。你的意思是编辑现有的实例吗?在这种情况下,你可以把'old('username')'放在表单字段中,就像这样'' 。这里是[旧输入文档](https://laravel.com/docs/5.4/requests#old-input) –

+0

我整理了问题。现在一切都像魅力工作。谢谢你:) – 06011991