uniapp 上传图片 laravel8.5报错:ErrorException: Header may not contain more than a single header
1). 当前使用的 Laravel 版本?
8.5
2). 当前使用的 php/php-fpm 版本?
7.3.4
3). 当前系统
windows 10
前端使用 uniapp 上传图片后 laravel 报错
ErrorException: Header may not contain more than a single header, new line detected in file E:\han\goodlife\vendor\symfony\http-foundation\Response.php on line 359
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, ‘Header may not …’, ‘E:\han\goodlife…’, 359, Array)
#1 E:\han\goodlife\vendor\symfony\http-foundation\Response.php(359): header(‘Location: http:…’, false, 302)
#2 E:\han\goodlife\vendor\symfony\http-foundation\Response.php(393): Symfony\Component\HttpFoundation\Response->sendHeaders()
#3 E:\han\goodlife\public\index.php(53): Symfony\Component\HttpFoundation\Response->send()
#4 {main}
但是原生的 php 就没问题,可以访问和保存图片。
使用接口工具测试也没什么问题。
服务端还没有到控制器就报错了
前端代码:
var that=this
uni.chooseImage({
count: 1, //默认9
sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: function (res) {
console.log(res.tempFiles[0])
var a=res.tempFiles[0].path+"/"+res.tempFiles[0].name
uni.uploadFile({
url:that.$baseurl+'/file-upload/image',//仅为示例,非真实的接口地址
filePath: res.tempFiles[0].path,
name: 'image',
formData:{
type:'profile',
},
success: (uploadFileRes) => {
console.log(uploadFileRes);
},
complete:err=>{
console.log(err)
}
});
}
})
服务端代码:
class FileUploadController extends Controller
{
public function image(Request $request) {
$validator = Validator::make($request->all(), [
'image' => 'required|image',
'type' => 'required',
]);
$errors = $validator->errors();
if ($errors->any())
return err($errors, 6001);
$user = $request->user();
$type = $request->type;
$store = [];
if ($type == 'store' || $type == 'goods') {
$store = $user->stores()->find($request->store_id);
if (empty($store)) {
return err('没有找到该店铺');
}
}
if ($request->file('image')->isValid()) {
$upload_path = 'images/';
if ($request->type == 'profile') {
$upload_path .= 'profile/' . date('Y/m/d',time()); //头像
} else if ($request->type == 'store_info') {
$upload_path .= 'store_info/' . date('Y/m',time()) . '/';
} else if ($request->type == 'store') {
$upload_path .= 'store/' . $store['id'] . '/';
} else {
return err('type参数错误');
}
$path = $request->image->store($upload_path, 'public');
return res(['url' => file_url($path), 'path' => $path]);
}
return err('上传错误');
}
}
就觉得这个问题很诡异,网上也没搜到有合适的解决方法,希望大神能够指点一二
推荐文章: