我目前正在尝试使用Slim框架发布表单后将文件上传到Parse。出于某种原因,我得到了文件名,但实际上无法使用我当前的设置获取文件。我也试过寻找$_FILES
阵列,但没有任何事情发生。有什么我想念的吗?这是我目前的路线:
$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());
}
});
});
答案 0 :(得分:0)
不确定这是否完全是实现此目的的正确方法,但这就是我开始工作的方式。如果有人有更好的想法,我会全力以赴......
我将此添加到我的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
}