我不確定為什么null在嘗試測試我的端點是否有效時會得到回報。表名是正確的,但由于某種原因,它無法識別我的文件。盡管我有代碼,但我的表有不止一列,并且是使用php artisan. (我只是想填充filePath列,稍后我將處理其他列)。下面是一張圖片供參考如果我刪除dd($filePath);控制器中的which - 郵遞員錯誤將返回:Integrity constraint violation: 1048 Column 'file_path' cannot be null (SQL: insert into 照片 (文件路徑) values (?))注意:我的 Laravel 代碼庫和 React.js 代碼庫是完全分開的。任何人都知道為什么會發生這種情況?這是我的前端代碼:fileSelectedHandler = event => { this.setState({ selectedFile: event.target.files[0] })}fileUploadHandler = () => { const fd = new FormData(); fd.append('image', this.state.selectedFile, this.state.selectedFile.name); axios.post('http://myendpoint/api/auth/wall-of-fame', fd) .then(res => { console.log(res); })}<form action="http://myendpoint/api/auth/wall-of-fame" encType='multipart/form-data' id="login-form" className="form"> <input type="file" name="file" onChange={this.fileSelectedHandler}/> <button onClick={this.fileUploadHandler}>Upload</button> </form>這是我的 php 控制器:public function store(Request $request){ $filePath = $request->input('file')->getClientOriginalName(); $data=array('file_path'=>$filePath); // dd($filePath); DB::table('photos')->insert($data); echo "Record inserted successfully.<br/>"; echo '<a href = "/insert">Click Here</a> to go back.';}
1 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
當從Request檢索文件時,使用file
method 而不是input
method :
$filePath?=?$request->file('file')->getClientOriginalName();
您可以使用以下方法確定請求中是否存在文件hasFile
:
if?($request->hasFile('file'))?{ ????$filePath?=?$request->file('file')->getClientOriginalName(); }
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消