首页  >  问答  >  正文

如何在 Laravel 9 中将 json 数据和表单数据发布到单个请求中

我有一个表单,在表单内我可以输入多个图像,并将其转换为 json

我的表单 html create.blade.php

<form method="post" action="{{ route('m_announcement.store') }}" enctype="multipart/form-data">
                @csrf
/////// many input just not included 
//////// my image input
                 <div class="row mb-4">
                    <label class="col-sm-2 col-label-form">Upload Images</label>
                    <div class="col-sm-10">
                        <input type="file" name="files[]" id="files" multiple
                            accept="image/jpeg, image/png, image/gif," onchange="timeFunction()"><br />
                    </div>
                </div>

///////// my imagae preview
                <output id="Filelist"></output>

                <div class="text-center">
                    <input type="submit" class="btn btn-primary" value="Add" />
                </div>

</form>

脚本中的此函数以 json 格式将图像输出到 var AttachmentArray,我也想通过表单发布

function printvalues() {
            console.log(AttachmentArray); all image converted in base64 output in json format with imahe details

有没有办法将 json 连接到表单,当我按提交 json 时,数据将根据请求输出

laravel 9 公告控制器.php

public function store(Request $request)
    {
      
        //dd($request);
        $jsonimages = $request->jsonimages;

      // decode the json a create loop to upload to database image
      // some code to upload the form to announcement database

        //image upload
}

我不只是在前端,大多数时候我首先在后端开发,这是我的第一个网络应用程序项目,我尝试搜索ajax或其他东西,但我还不明白。

P粉083785014P粉083785014265 天前468

全部回复(1)我来回复

  • P粉518799557

    P粉5187995572024-01-03 00:13:52

    您可以使用以下方式获取字段输入的名称

    dd($request->file->('files'));

    请参阅此处了解更多信息在 laravel 9 中检索上传的文件

    回复
    0
  • 取消回复