Angular 发送带有文件的表单数据时,我做错了什么
在下面的版本中,我将发送数据。无法发送该文件Angular 发送带有文件的表单数据时,我做错了什么,angular,Angular,在下面的版本中,我将发送数据。无法发送该文件 onSubmit() { const fd = new FormData(); fd.append('resume', this.chosenFile); const data = this.contactForm.value; data['resume'] = fd; console.log(data); this.loading = true; this.data.createJobEnqu
onSubmit() {
const fd = new FormData();
fd.append('resume', this.chosenFile);
const data = this.contactForm.value;
data['resume'] = fd;
console.log(data);
this.loading = true;
this.data.createJobEnquiry(data).subscribe((res)=>{
this.loading = false;
},(err)=>{
this.loading = false;
console.log(err)
alert("Something went wrong");
});
}
在下面的代码中,我只发送fd,它是文件,并且被发送
onSubmit() {
const fd = new FormData();
fd.append('resume', this.chosenFile);
this.loading = true;
this.data.createJobEnquiry(fd).subscribe((res)=>{
this.loading = false;
},(err)=>{
this.loading = false;
console.log(err)
alert("Something went wrong");
});
}
HTML:
我是如何得到文件的。我得到正确的文件,所以没有问题
onFileSelected(event) {
this.chosenFile = <File>event.target.files[0];
}
onFileSelected(事件){
this.chosenFile=event.target.files[0];
}
在第一个错误中,它给出了错误“Resume is required”,因此它根本没有被发送,在第二个错误中,没有错误文件被正确发送,错误不能在服务中。错误在于我将简历字段指定给文件的方式
this.contactForm.value
是包含所有表单数据的对象,包括简历文件,该文件最初只有文件路径。
此处数据['resume']=fd;我只是把文件分配到简历栏。我知道错误就在某个地方,我可能做得不对。
如果我硬编码
data['resume']=“dd”
则服务器的错误仅允许使用pdf文件,这进一步证明我在使用数据时没有正确发送文件 那么这里的问题是什么?这个createJobInquiry()
方法看起来怎么样?控制台中有错误吗?API是从Postman调用的吗?在这里发布您的服务函数代码第一个它给出错误“Resume is required”,因此它没有被正确发送,第二个没有错误。CreateJobInquiry(formDetails:any){返回this.http.post(this.CreateJobInquirryAPI,formDetails);}
@zuyi是从邮递员那里调用API的吗?那么这里的问题是什么?这个createJobInquiry()
方法看起来怎么样?控制台中有错误吗?API是从Postman调用的吗?在这里发布您的服务函数代码第一个它给出错误“Resume is required”,因此它没有被正确发送,第二个没有错误。CreateJobInquiry(formDetails:any){返回this.http.post(this.CreateJobInquirryAPI,formDetails);}
@zuyi是从邮递员那里调用API的吗?
onFileSelected(event) {
this.chosenFile = <File>event.target.files[0];
}