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);
}
任何幫助,將不勝感激。謝謝您。
它的工作將數據插入。但當我試圖編輯形式,其存儲爲空值。我不明白爲什麼? – 06011991
我很高興它的工作。你的意思是編輯現有的實例嗎?在這種情況下,你可以把'old('username')'放在表單字段中,就像這樣'' 。這裏是[舊輸入文檔](https://laravel.com/docs/5.4/requests#old-input) –
我整理了問題。現在一切都像魅力工作。謝謝你:) – 06011991