是否可以通过来自 $_FILES 的 Angular9 中的 http 请求访问上传的文件?
Is it possible to reach the uploaded file by an http request in Angular 9 from $_FILES?
我试图通过 angular 9 中的 http 请求将文件上传到 php 服务器,但服务器无法在 $_FILES 中接收上传的文件。我写的代码有点像:
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); }
);
}
当我上传文件时,我收到以下消息:
上传期间出错:[对象对象]。
有没有可能从服务器端的 $_FILES 到达上传的文件?
提前致谢。
将您的文件附加到 Formdata
并发送
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); }
);
}
我试图通过 angular 9 中的 http 请求将文件上传到 php 服务器,但服务器无法在 $_FILES 中接收上传的文件。我写的代码有点像:
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); }
);
}
当我上传文件时,我收到以下消息: 上传期间出错:[对象对象]。 有没有可能从服务器端的 $_FILES 到达上传的文件? 提前致谢。
将您的文件附加到 Formdata
并发送
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); }
);
}