使用Laravel 5.6作为后端,并使用Angular 6作为前端
我想上传文件,但没有发生
我在laravel控制器中将formData
设为空。
console.log
可以正常显示
File(674)
这是我调用laravel的函数。
signup(input: any, fileToUpload: File) {
let promise = new Promise((resolve, reject) => {
let apiURL = `${this.signupApi}`;
let formData: FormData = new FormData();
formData.append('fileKey', fileToUpload);
console.log(fileToUpload);
this.httpClient.post(apiURL, { input, formData })
.toPromise()
.then(
res => { // Success
if (res['code'] == 'taken') {
resolve('taken');
} else if (res['code'] !== 500) {
resolve(true);
} else {
// return false to indicate fail
resolve(false);
}
},
msg => { // Error
reject(msg);
});
});
return promise;
}
这是调用服务功能的功能
fileToUpload: File = null;
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
}
this.signupCharityService.signup(this.input, this.fileToUpload).then(data => {
if (data == 'taken') {
this.haveTaken = true;
setTimeout(() => {
this.haveTaken = false;
}, 3000);
} else if (data == true) {
this.flashMessagesService.show('Signup Success. Check your email for activation link.', { cssClass: 'alert-success', timeout: 5000 });
this.router.navigateByUrl('/');
} else {
this.haveError = true;
setTimeout(() => {
this.haveError = false;
}, 3000);
}
})
HTML
<div class="file-ulload-outer">
<div class="custom-file-upload">
<input data-type="file-upload" type="file" class="form-control" (change) = "handleFileInput($event.target.files)" (keypress) = "onChange()"/>
</div>
</div>
我认为这与FormData有关,我不知道。
答案 0 :(得分:0)
我不为什么会引起问题,但是当我只发送formData而没有输入时,它就起作用了。像这样.post(apiURL, formData)