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('上传错误');
    }
}

就觉得这个问题很诡异,网上也没搜到有合适的解决方法,希望大神能够指点一二

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

贴 PHP 代码, 小程序代码没看出问题

3年前 评论
讨论数量: 6

贴 PHP 代码, 小程序代码没看出问题

3年前 评论
Location: http:…’, false, 302

文件是上传成功了, response 的时候出问题了

3年前 评论

@crazy 你好 代码贴上了,主要是还没执行到控制器就报错啦

3年前 评论
sodasix 3年前
han123 (作者) (楼主) 3年前

@crazy 前端没有穿token,其他接口不传token报401,这个图片上传报500,就没往这块想。花了我两天时间,太粗心了

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!