我有Laravel应用,因此我需要上传图片类别,我使用FilePond库
当我在前端上传图像时,库的一切都很好,但是当对我的请求进行dd()
时,我得到了这个数组:
array:5 [▼
"_token" => "KyEk4ha6UbMSG0WfYjD84wcUZU188pv1OK5utwrt"
"name" => "d"
"category_news_id" => "6"
"desc" => "f"
"image" => null
]
图像问题始终显示为null,即使我上传了图像!例如:
html代码:
<input type="file" name="image" class="my-pond">
$(function(){
FilePond.registerPlugin(FilePondPluginImagePreview);
$('.my-pond').filepond();
});
答案 0 :(得分:1)
刀片文件
<label>Upload Image:</label>
<input type="file" class="filepond" name="image" accept="image/png, image/jpeg, image/gif"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>
<!-- include FilePond plugins -->
<script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>
<!-- include FilePond jQuery adapter -->
<script src="https://unpkg.com/jquery-filepond/filepond.jquery.js"></script>
<script>
FilePond.registerPlugin();
var element = document.querySelector('meta[name="csrf-token"]');
var csrf = element && element.getAttribute("content");
FilePond.setOptions({
server: {
url: "{{ url('upload')}}",
process: {
headers: {
'X-CSRF-TOKEN': csrf
},
}
}
});
const inputElement = document.querySelector('input[name="image"]');
const pond = FilePond.create( inputElement);
</script>
路由文件:
Route::post('upload', 'AccountController@imageup')->name('upload');
控制器逻辑:
public function imageup(Request $request) {
if($request->hasFile('image'))
{
$file = $request->file('image');
$filenameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename . '_' . date('mdYHis') . uniqid() . '.' . $extension;
$path = $request->file('image')->storeAs('public/profile-image/', $fileNameToStore);
return $data['image'] = $fileNameToStore;
}
}
将文件名插入数据库:
$user->image = $request->get('image');
$user->save();