我用的uniapp中的uview想弄一个上传图片的功能
前端上传图片的代码:
uploadFilePromise(url) {
return new Promise((resolve, reject) => {
let a = uni.uploadFile({
url: 'http://localhost:8000/api/Ajax/uploadx', // 仅为示例,非真实的接口地址
filePath: url,
method:'post',
header:{
'Content-Type':'multipart/form-data'
},
name: 'file',
formData: {
user: 'test'
},
success: (res) => {
resolve(res.data.data)
},
fail:(res)=>{
reject(res)
}
});
})
},
后端用的buildadmin:
public function uploadx()
{
$file = $this->request->file('file');
$userId= $this->request->post('user_id');
try {
$upload = new Upload($file);
$attachment = $upload->upload(null, 0, intval($userId));
unset($attachment['create_time'], $attachment['quote']);
} catch (Throwable $e) {
$this->error($e->getMessage());
}
$this->success(__('File uploaded successfully'), [
'file' => $attachment ?? []
]);
}
最后提示:Provisional headers are shown
没有任何返回信息,但这接口在apifox里测试是没有问题的。
其中线索:上传时有这两个请求,有下面的提示。
因为我用的buildadmin后端框架,测试时需要在header里传server=1