我正在尝试使用laravel作为后端框架从react-native项目上传图像。->这是我通过抓取发送的内容。->这是该错误我明白了 这是我的Laravel代码:
public function upload(Request $request)
{
$image = $request->get('data');
$name = 'Sup.bitch';
Image::make($request->get('data'))->save(public_path('images/').$name);
$fileupload = new Fileupload();
$fileupload->filename=$name;
$fileupload->save();
return response()->json(['message' => 'Success']);
}
这是我的前端代码:
myfun = () => {
ImagePicker.showImagePicker(options, response => {
console.log("Response = ", response);
if (response.didCancel) {
console.log("User cancelled image picker");
} else if (response.error) {
console.log("Image Picker Error: ", response.error);
} else {
let source = { uri: response.uri };
this.setState({
avatarSource: source,
pic: response.data
});
}
});
};
uploadPic = () => {
console.log(this.state.pic);
alert("ddf");
fetch(
"POST",
"http://192.168.2.45/backend/public/api/upload",
{
Authorization: "Bearer access-token",
otherHeader: "foo",
"Content-Type": "multipart/form-data"
},
[
// element with property `filename` will be transformed into `file` in form data
{ name: "image", filename: "avatar.png", data: this.state.pic }
]
).then(resp => {
console.log(resp.message);
alert("your image uploaded successfully");
this.setState({ avatarSource: null });
});
};