1
현재 저는 ES6 + PHP에서 단일 페이지 응용 프로그램을 작성 중이며 ajax 호출에 몇 가지 문제가 있습니다. 가져 오기 API를 통해 파일 업로드 예제를 찾을 수 없으며 솔직히 아약스 호출이 PHP에서 데이터를 읽는 방법 때문에 이런 식으로 표시되어야한다는 단서가 없습니다.단일 페이지 응용 프로그램에서 파일 업로드
이와 비슷한 형식은 백엔드에 양식을 보내야합니다. 이것은 내가 지금까지 무엇을 가지고,하지만 작동하지 않습니다 깨끗한 솔루션 :(
JS 생각할 수 없다 :
const headers = new Headers({
'Accept':'application/json',
'Content-Type':'application/json'
});
class User{
constructor(){
this._ajaxData = {};
}
/**
* @param {object} curObj
* @param {int} curObj.ID
* @param {HTMLElement} curObj.InputDate
* @param {HTMLElement} curObj.Username
* @param {HTMLElement} curObj.UploadFile = <input type='file'>
*/
collectInputData(curObj){
this._ajaxData = {
UserID: curObj.ID,
ChangeDate: curObj.InputDate.value,
Username: curObj.Username.value,
SomeFile: curObj.UploadFile
};
}
doAjax(){
let _ajaxData = this._ajaxData;
let request = new Request("ajax/saveUser.php", {
method : "POST",
headers: headers,
body : JSON.stringify(_ajaxData)
});
fetch(request).then(function (res) {
return res.json();
}).then(function (data) {
console.log(data);
});
}
}
PHP :
require_once __DIR__.'/../vendor/autoload.php';
$PDO = \DBCon::getInstance();
$data = json_decode(file_get_contents('php://input'));
$PDO->beginTransaction();
$_FILES["fileToUpload"]["name"]
$User = new \User();
$User->setUserID($data->UserID);
$User->setChangeDate($data->ChangeDate);
$User->setUsername($data->Username);
/**
* to use like with $_FILES["fileToUpload"]
*
* @param array $data->SomeFile
*/
$User->setUploadFiles($data->SomeFile);
$User->save();
try{
$PDO->commit();
echo true;
}catch(PDOException $e){
echo $e->getMessage();
}