我正在嘗試通過(guò) angular 9 中的 http 請(qǐng)求將文件上傳到 php 服務(wù)器,但服務(wù)器無(wú)法在 $_FILES 中接收上傳的文件。我寫(xiě)的代碼有點(diǎn)像:HTML:<input type="file" (change)="detectFiles($event)" name="testFile" >PHP:<?php header("Access-Control-Allow-Origin: *"); header('Access-Control-Allow-Credentials: true'); header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE"); header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); echo json_encode($_FILES['testFile']['name']);?>打字稿:detectFiles(event) { const file = event.target.files[0]; this.httpClient.post<any>('http://localhost/test/uploadFile.php', file ) .subscribe( (response) => { console.log('uploading is done: ' + response); }, (error) => { console.log('ERR during the uploading: ' + error); } );}當(dāng)我上傳文件時(shí),我收到以下消息: 上傳期間出錯(cuò):[object Object]。有沒(méi)有可能從服務(wù)器端的 $_FILES 到達(dá)上傳的文件?提前致謝。
1 回答

慕仙森
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
將您的文件附加到Formdata并發(fā)送
detectFiles(event) {
var formData = new FormData();
formData.append("file", event.target.files[0]);
this.httpClient.post<any>('http://localhost/test/uploadFile.php', formData)
.subscribe( (response) => { console.log('uploading is done: ' + response); },
(error) => { console.log('ERR during the uploading: ' + error); }
);
}
- 1 回答
- 0 關(guān)注
- 128 瀏覽
添加回答
舉報(bào)
0/150
提交
取消