我不确定为什么null
尝试测试端点是否有效时会得到回报。
表名正确,但是由于某种原因,它无法识别我的文件。尽管有我的代码,但我的表还是有多个列,并且是使用创建的php artisan
。(我只是想填充filePath
列,我将在以后处理其他列)。以下是供参考的图片
如果我摆脱了dd($filePath);
控制器中的那个-邮递员错误将返回:
Integrity constraint violation: 1048 Column 'file_path' cannot be null (SQL: insert into
照片 (
file_path) 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.';
}
从Request检索文件时,请使用file
方法而不是input
method:
$filePath = $request->file('file')->getClientOriginalName();
您可以使用以下hasFile
方法确定请求中是否存在文件:
if ($request->hasFile('file')) {
$filePath = $request->file('file')->getClientOriginalName();
}
在这里查看官方文档
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句