Heim  >  Fragen und Antworten  >  Hauptteil

So veröffentlichen Sie JSON-Daten und Formulardaten in einer einzigen Anfrage in Laravel 9

Ich habe ein Formular, in das ich mehrere Bilder eingeben und in JSON konvertieren kann

Mein Formular-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>

Diese Funktion im Skript gibt das Bild im JSON-Format an ein var AttachmentArray aus, das ich auch über das Formular posten möchte

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

Gibt es eine Möglichkeit, den JSON mit dem Formular zu verbinden? Wenn ich auf „JSON senden“ drücke, werden die Daten gemäß der Anfrage ausgegeben

laravel 9 Announcement Controller.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
}

Ich arbeite nicht nur am Frontend, die meiste Zeit entwickle ich zuerst am Backend. Dies ist mein erstes Web-App-Projekt. Ich habe versucht, nach Ajax oder so etwas zu suchen, aber ich verstehe es noch nicht.

P粉083785014P粉083785014265 Tage vor466

Antworte allen(1)Ich werde antworten

  • P粉518799557

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

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

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

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

    Antwort
    0
  • StornierenAntwort