donglv6960 2015-05-19 14:40
浏览 44

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

I'm currently trying to upload a file to Parse following the posting of a form with the Slim framework. 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. 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());
        }
    });
});
  • 写回答

1条回答 默认 最新

  • dronthpi05943 2015-05-20 16:07
    关注

    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.

    // 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
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统