$ _FILES使用DropzoneJS和Symfony 1.4返回空

时间:2022-10-25 19:11:06

I've scoured through several SO and blog posts online but can't find something that works.

我已经在网上浏览了几篇SO和博客文章,但找不到有用的东西。

I'm trying to set up a simple HTML drag and drop form where users can upload several files at once via DropzoneJS.

我正在尝试设置一个简单的HTML拖放表单,用户可以通过DropzoneJS一次上传多个文件。

HTML:

<form action='<?php echo url_for("@menu_basic_menu"); ?>' method="post" enctype="multipart/form-data" class="dropzone" id="basic_menu_dropzone"></form>

<button id="file_submit_btn" type="submit" form='basic_menu_dropzone' value="submit">SUBMIT</button>

Javascript:

jQuery(document).ready(function($) {
    Dropzone.autoDiscover = false;

    var dropzone = new Dropzone('#basic_menu_dropzone', {
        paramName: 'files',
        addRemoveLinks: true,
        uploadMultiple: true,
        autoProcessQueue: false,
    });

    $('#file_submit_btn').click(function() {
        dropzone.processQueue();
    });
});

Here you'll notice that I also set up a submit button such that we only start the upload process of the files on submit

在这里您会注意到我还设置了一个提交按钮,以便我们只在提交时启动文件的上传过程

PHP (snippet of actions.class.php):

public function executeBasicMenu(sfWebRequest $request) {
    if ($request->isMethod('post')) {
        print_r_tree($_FILES);
    }
  }

I have everything setup so that executeBasicMenu is properly fired on submit, but $_FILES always returns an empty array.

我有一切设置,以便在提交时正确触发executeBasicMenu,但$ _FILES总是返回一个空数组。

Notes:

  • If I replace the dropzone form with a regular input type='file' tag then everything works, so my gut feeling is telling me my configuration with dropzone is wrong somewhere.
  • 如果我用常规输入type ='file'标签替换dropzone表单,那么一切正常,所以我的直觉就是告诉我我的配置dropzone在某处是错误的。

  • I've stepped through the dropzone.js source code and it looks like right when it's about to send the data on line 1386: xhr.send(formData), formData is empty.
  • 我已经逐步完成了dropzone.js源代码,当它即将在1386行发送数据时它看起来正确:xhr.send(formData),formData为空。

Any help is deeply appreciated!

任何帮助深表感谢!

1 个解决方案

#1


0  

Finally found my mistakes!

终于找到了我的错误!

In my code, I was submitting the form on the button click:

在我的代码中,我在单击按钮上提交表单:

<button id="file_submit_btn" type="submit" form='basic_menu_dropzone' value="submit">SUBMIT</button>

But I was also trying to call processQueue() on the same button click. However, the form would be submitted before the files would get sent so no files would be received on the PHP side.

但我也试图在同一个按钮点击上调用processQueue()。但是,表单将在文件发送之前提交,因此PHP端不会收到任何文件。

I also incorrectly instantiated my dropzone instance - I should have included a parallelUploads field so that I could upload multiple files at once.

我也错误地实例化了我的dropzone实例 - 我应该包含一个parallelUploads字段,以便我可以一次上传多个文件。

So with my new updated code, everything works as expected and I can get a handle on my files on the PHP side!

因此,使用我新的更新代码,一切都按预期工作,我可以在PHP端处理我的文件!

Solution

Html

<form action='<?php echo url_for("@menu_basic_menu"); ?>'method="post" enctype="multipart/form-data" class="dropzone" id="basic_menu_dropzone"></form>
<button id="file_submit_btn" type="button" form='basic_menu_dropzone' value="submit">SUBMIT</button>

JS

jQuery(document).ready(function($) {
    Dropzone.autoDiscover = false;

    var dropzone = new Dropzone('#basic_menu_dropzone', {
        paramName: 'files',
        addRemoveLinks: true,
        uploadMultiple: true,
        autoProcessQueue: false,
        parallelUploads: 10
    });

    $('#file_submit_btn').click(function() {
        dropzone.processQueue();
    });
});

PHP

public function executeBasicMenu(sfWebRequest $request) {
    foreach($_FILES['files']['name'] as $index => $tmpName) {
      error_log($tmpName);
    }
}

Hope this helps others too!

希望这对其他人也有帮助!

#1


0  

Finally found my mistakes!

终于找到了我的错误!

In my code, I was submitting the form on the button click:

在我的代码中,我在单击按钮上提交表单:

<button id="file_submit_btn" type="submit" form='basic_menu_dropzone' value="submit">SUBMIT</button>

But I was also trying to call processQueue() on the same button click. However, the form would be submitted before the files would get sent so no files would be received on the PHP side.

但我也试图在同一个按钮点击上调用processQueue()。但是,表单将在文件发送之前提交,因此PHP端不会收到任何文件。

I also incorrectly instantiated my dropzone instance - I should have included a parallelUploads field so that I could upload multiple files at once.

我也错误地实例化了我的dropzone实例 - 我应该包含一个parallelUploads字段,以便我可以一次上传多个文件。

So with my new updated code, everything works as expected and I can get a handle on my files on the PHP side!

因此,使用我新的更新代码,一切都按预期工作,我可以在PHP端处理我的文件!

Solution

Html

<form action='<?php echo url_for("@menu_basic_menu"); ?>'method="post" enctype="multipart/form-data" class="dropzone" id="basic_menu_dropzone"></form>
<button id="file_submit_btn" type="button" form='basic_menu_dropzone' value="submit">SUBMIT</button>

JS

jQuery(document).ready(function($) {
    Dropzone.autoDiscover = false;

    var dropzone = new Dropzone('#basic_menu_dropzone', {
        paramName: 'files',
        addRemoveLinks: true,
        uploadMultiple: true,
        autoProcessQueue: false,
        parallelUploads: 10
    });

    $('#file_submit_btn').click(function() {
        dropzone.processQueue();
    });
});

PHP

public function executeBasicMenu(sfWebRequest $request) {
    foreach($_FILES['files']['name'] as $index => $tmpName) {
      error_log($tmpName);
    }
}

Hope this helps others too!

希望这对其他人也有帮助!