我已经搜索了很多,并找到了一个合适的代码,它应该可以正常工作,但它会给我错误。
uploadpic.html 代码:
<input type="file" (change)="fileChange($event)" placeholder="Upload file" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png">
uploadpic.ts 如下
fileChange(event) {
let fileList: FileList = event.target.files;
if(fileList.length > 0) {
let file: File = fileList[0];
let formData:FormData = new FormData();
formData.append('uploadFile', file, file.name);
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
// let options = new RequestOptions({ headers: headers });
this.http.post(`http://funiks.com/adminv8/products-api/upload.php`, formData, { headers: headers })
.map(res => res.json())
// .catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}
}
当我检查我的服务器文件没有上传时,我在我的控制台中收到此消息
SyntaxError: Unexpected token S in JSON at position 0 at Object.parse (<anonymous>) at Response.Body.json (http.es5.js:800) at MapSubscriber.project (operationadd.component.ts:49) at MapSubscriber._next (map.js:77) at MapSubscriber.Subscriber.next (Subscriber.js:89) at XMLHttpRequest.onLoad (http.es5.js:1229) at ZoneDelegate.invokeTask (zone.js:424) at Object.onInvokeTask (core.es5.js:4140) at ZoneDelegate.invokeTask (zone.js:423) at Zone.runTask (zone.js:191)
我的PHP代码适用于html表单,需要使用angular2
这是我的php,你需要它 - https://pastebin.com/ND4G9RJk