z-song / laravel-admin

Build a full-featured administrative interface in ten minutes
https://laravel-admin.org
MIT License
11.15k stars 2.82k forks source link

步骤表单文件/图片上传的问题 #5732

Closed toproplus closed 1 year ago

toproplus commented 1 year ago

Description:

在步骤表单的步骤一上传单图或多图,请问如何保存临时上传的文件对象到步骤二使用? 而$this->next($data = []); 只能保存数组的字符串值,难道要先上传后拿到上传地址再跳步骤二?

alexoleynik0 commented 1 year ago

You're right, the file can't be session-stored between steps without saving/storing on the server/s3/etc, because Serialization of 'Illuminate\Http\UploadedFile' is not allowed (that's if you try to do return $this->next($request->all()); from file-uploading StepForm@handle).

The only way to accomplish something like that is to store file on one step, and use its path on another, as you rightfully suggested. StepForm with file upload:

class Upload extends \Encore\Admin\Widgets\StepForm {
    public $title = 'Upload step';
    public function handle(\Illuminate\Http\Request $request) {
        $res['myfile'] = $request->file('myfile')->store('temp');
        return $this->next($res);
    }
    public function form() {
        $this->image('myfile'); // used image here, will be similar with `file`
    }
}

The any next StepForm that needs to use saved/stored file:

class Details extends \Encore\Admin\Widgets\StepForm {
    public $title = 'Details step';
    public function form() {
        $this->text('name');
        // use that session-stored data from "Upload" step in `form`, `handle`, etc. 
        $this->html('<img src="/storage/'. $this->all()['upload']['myfile'] . '" />');
    }
}

That should work just ok. Let me know if you have something else in mind. But I'm still afraid you are obligated to store file between steps, I see no other way around.

toproplus commented 1 year ago

Thanks for your answer, I'm got it . Good Luck to you !