代码之家  ›  专栏  ›  技术社区  ›  HectoB

使用symfony[duplicate]通过AJAX发送文件

  •  0
  • HectoB  · 技术社区  · 2 年前

    我用的是Symfony 2.7

    我有这张表格

    <form name="Document" method="post" action enctype="multipart/form-data">
        <div class="col-md-6">
            <input type="file" id="documento" name="Document[Ruta]" required="required">
        </div>
        <div class="col-md-3">
            <button type="button" onclick="saveDoc()" class="btn btn-success" data-dismiss="modal">Subir</button>
        </div>
    </form>
    

    这是ajax:

    function saveDoc() {
        var inputFile = document.getElementById('documento');
        var file = inputFile.files[0];
        var data = new FormData();
        data.append('doc', file, file.name);
        $.ajax({
            url: "{{ path('fileupload') }}",
            type: "POST",
            data: {'data': data},
            processData: false,
            contentType: false,
            cache: false
        });
    
    }
    

    使用此symfony控制器:

    public function fileUpload(Request $request)
    {
        $file = $request->files->get('doc')->getClientOriginalName;
        $user = $this->getUser();
    
        $filename = md5(uniqid()).'.'.$file->guessExtension();
    
        $request->files->get('doc')->move(
            $this->getParameter('upload_directory'),
            $filename
        );
        $document = new Document();
        $document->setRuta($filename);
        $document->setIdUser($user);
        $document->setFechaCreacion(new \DateTime("now"));
        $em = $this->getDoctrine()->getManager();
        $em->persist($document);
        $em->flush();
    
        return new JsonResponse(true);
    
    }
    

    但我有一个错误:

    注意:尝试获取非对象的属性 500内部服务器错误-ContextErrorException

    我看到235897页用symfony和ajax上传文件,我不明白我的代码出了什么问题,你们能帮我吗?

    0 回复  |  直到 6 年前
        1
  •  2
  •   Mahdi    5 年前

    改变这个

    function saveDoc() {
        var inputFile = document.getElementById('documento');
        var file = inputFile.files[0];
        var data = new FormData();
        data.append('doc', file, file.name);
        $.ajax({
            url: "{{ path('fileupload') }}",
            type: "POST",
            data: {'data': data},
            processData: false,
            contentType: false,
            cache: false
        });
    
    }
    

    function saveDoc() {
        var inputFile = document.getElementById('documento');
        var file = inputFile.files[0];
        var data = new FormData();
        data.append('doc', file, file.name);
        $.ajax({
            url: "{{ path('fileupload') }}",
            type: "POST",
            data: data,
            processData: false,
            contentType: false,
            cache: false
        });
    
    }
    

    在控制器里

    更改以下行:

    $file = $request->files->get('doc')->getClientOriginalName;
    

    致:

    $file = $request->files->get('doc');
    
        2
  •  0
  •   habibun    6 年前

    改变

    $file = $request->files->get('doc')->getClientOriginalName;
    

    $file = $request->files->get('doc');