简体   繁体   English

从POST获取文件以使用Slim框架上传到Parse

[英]Getting file from POST to upload to Parse with Slim framework

I'm currently trying to upload a file to Parse following the posting of a form with the Slim framework. 我目前正在尝试在使用Slim框架发布表单之后将文件上传到Parse。 For some reason I get the file name, but can't actually get the file with my current setup. 由于某种原因,我得到了文件名,但实际上无法使用当前设置获得文件。 I've also tried looking for the $_FILES array, but nothing is coming across. 我也尝试过寻找$_FILES数组,但是什么也没碰到。 Is there something I'm missing? 有什么我想念的吗? Here's my current route: 这是我目前的路线:

$app->group('/settings', function () use ($app) {
    $app->post('/update(/:settingsId)', function ($settingsId = null) use ($app) {
        $query = new ParseQuery("Settings");
        // Get a specific object:
        $settings = $query->get($settingsId);

        // Set values:
        $settings->set("appName", $app->request->post('appName'));
        $settings->set("directorName", $app->request->post('directorName'));
        $settings->set("mkUsername", $app->request->post('mkUsername'));
        $settings->set("mkPassword", $app->request->post('mkPassword'));

        error_log(print_r($_FILES, true));

        // see if we have a file
        if ($app->request->post('appBackgroundImage')) {
            // save file to Parse
            $appBackgroundImage = ParseFile::createFromData(file_get_contents($_FILES["file"]["tmp_name"]), $app->request->post('appBackgroundImage'));
            $appBackgroundImage->save();

            $settings->set("appBackgroundImage", $appBackgroundImage);
        }

        try {
            $settings->save();
            $app->redirect('/settings');
        } catch (ParseException $ex) {
            $app->redirect('/settings/' . $ex->getCode());
        }
    });
});

Not sure if this is entirely the correct way to accomplish this, but this is how I got it to work. 不知道这是否完全是完成此操作的正确方法,但这就是我如何使它起作用。 If someone has a better idea, I'd be all ears... 如果有人有更好的主意,我会全力以赴...

I added this to my JS so it actually posts the form via AJAX and pushes the file along with the normal form contents as well. 我将其添加到我的JS中,因此它实际上是通过AJAX发布表单的,​​并将文件与常规表单内容一起推送。

// File Uploader
        var form = document.getElementById('settings-form');
        form.onsubmit = function() {
          var formData = new FormData(form);

          formData.append('appBackgroundImage', file);

          var xhr = new XMLHttpRequest();
          // Add any event handlers here...
          xhr.open('POST', form.getAttribute('action'), true);
          xhr.send(formData);

          return false; // To avoid actual submission of the form
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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