Javascript 如何从请求中获取上载的文件?

Javascript 如何从请求中获取上载的文件?,javascript,php,symfony,upload,dropzone.js,Javascript,Php,Symfony,Upload,Dropzone.js,当我试图从请求中获取上传的文件时,为什么总是在dump()上获取NULL 我试图将dropzone链接到一个Symfony表单,但失败了(无法在彼此内部创建两个表单) 所以我做了一个新的控制器测试(我尽可能地简化它) 如何使用控制器中的dropzone检索上载的图像 控制器代码: <?php namespace App\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Rout

当我试图从请求中获取上传的文件时,为什么总是在
dump()
上获取
NULL

我试图将dropzone链接到一个Symfony表单,但失败了(无法在彼此内部创建两个表单)

所以我做了一个新的控制器测试(我尽可能地简化它)

如何使用控制器中的dropzone检索上载的图像

控制器代码:

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class TestController extends AbstractController
{
    /**
     * @Route("/test", name="test")
     */
    public function index(Request $request)
    {
        dump($request->files->get('file'));

        return $this->render('test/index.html.twig');
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.css" integrity="sha256-e47xOkXs1JXFbjjpoRr1/LhVcqSzRmGmPqsrUQeVs+g=" crossorigin="anonymous" />
    <div class="row">
        <div class="col-sm-4">
            <form action="{{ path('test')}}" method="POST" enctype="multipart/form-data" class="dropzone">
            <div class="fallback">
                  <input type="file" name="file" />
            </div>
            </form>

        </div>
    </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js" integrity="sha256-cs4thShDfjkqFGk5s2Lxj35sgSRr4MRcyccmi0WKqCM=" crossorigin="anonymous"></script>

</body>
</html>

我猜想您正试图使用fetch API将此消息发送到服务器

如果希望使用Symfony的Request$Request->file->get('file'),则需要将请求主体包装在javascript中的FormData()对象中,如下所示:

function send() {

   const form = document.querySelector(`form`),
     body = new FormData(form);

   fetch(form.action, { // if you need the response, you can assign fetch to a variable
    method: `POST`,
    body
   });

}

console.log(myDropzone.files)使用此命令查看文件名。我应该在哪里准确地键入该行?现在可能转储整个$request对象以进行调试?可能您可以尝试转储$request->request->all()和$request->files->all()以查看到底发生了什么。