onFileChange(event) { const file = event.target.files[0]; const formData = new FormData(); formData.set('image', file); // 'image'为Laravel中文件输入框的name属性
this.http.post('your-api-endpoint', formData).subscribe(response => { console.log(response); }); }
public function store(Request $request) { $file = $request->file('image'); if ($file) { $filename = time() . '.' . $file->getClientOriginalExtension(); $file->move(public_path('uploads'), $filename); // 存储文件并返回响应 } else { return response()->json(['error' => '没有上传文件'], 400); } }
请注意,在Laravel 10中,您需要为上传文件提供文件系统驱动程序配置。请参考Laravel文档以了解更多信息。