简体   繁体   中英

Symfony4 (API) - Trying to upload a file using request->files->all() gives me an empty result

I'm trying to upload a file using API in symfony 4, i'm using RESTLET tool to send my file but all i got is a 200 OK but with an empty result.

Content-Type: text/plain

METHOD: POST

FILE SIZE: 1ko

Code:

<?php
namespace App\Controller\Api\Dossier;

use App\Entity\Dossier\Fichier;
use App\Helpers\Constant\ErrorMessage;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/{db}/fichier")
 */
class ApiFichierController extends ApiController
{
/**
 * @Route("/", name="api_fichier_enregistrer", methods={"POST"})
 * @param Request $request
 * @return JsonResponse|RedirectResponse
 */
public function retrieveAction(Request $request){
    // retrieve the file with the name given in the form.

    $filesResult = array();
    $filesBag = $request->files->all();

    foreach ($filesBag as $file){
        $filename = $file->getClientOriginalName();
        $filesResult []=  array(
            'path' => $file->getPathname(),
            'url'  => 'ddd'
        );
        $src    =  __DIR__;
        $file->move( $src ,$filename );

    }
    return new JsonResponse($filesBag);
}

Result

You want to use Content-Type: multipart/form-data in order to actually upload a file.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type#Content-Type_in_HTML_forms

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM