Symfony4 (API) - Попытка загрузить файл с помощью запроса-> файлы-> все () дает мне пустой результат - PullRequest
0 голосов
/ 24 сентября 2019

Я пытаюсь загрузить файл с помощью API в symfony 4, я использую инструмент RESTLET для отправки моего файла, но все, что я получил, это 200 OK, но с пустым результатом.

Content-Type: текст / обычный

МЕТОД: ПОСТ

РАЗМЕР ФАЙЛА: 1ko

Код:

<?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);
}

Результат

1 Ответ

0 голосов
/ 24 сентября 2019

Вы хотите использовать Content-Type: multipart / form-data для фактической загрузки файла.

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...