admin管理员组文章数量:1335582
I'm trying to upload a photo with some information. But stuck with the error "Argument of type 'File' is not assignable to parameter of type 'string'."
I am using angular 6 as frontent and as a backend, I'm using WebApi with SQL server 2012.
Thanks in advance guys and hoping for a soon response.
image-uploadponent.ts
imageUrl:String="";
fileToUpload:File=null;
handleImageChange(file: FileList){
this.fileToUpload = file.item(0);
var reader = new FileReader();
reader.onload=(event:any)=>{
this.imageUrl=event.target.result;
}
reader.readAsDataURL(this.fileToUpload);
}
uploadImage(imageData){
let name=imageData.name;
let number=imageData.number;
let price=imageData.price;
this.service.uploadImage(name,this.fileToUpload,number,price).subscribe(
data=>{
alert("successfully uploaded");
this.productForm.reset();
this.imageUrl="";
}
);
}
image-upload.service.ts
uploadImage(fileToUpload:File, imagename:string, num:string, price:string){
let formData:FormData = new FormData();
formData.append("file",fileToUpload,fileToUpload.name);
formData.append("Imagename",imagename);
formData.append("Number",num);
formData.append("Price",price);
return this.http.post(this.baseUrl+"UploadImage",formData,this.httpOptions);
}
Error:
I'm trying to upload a photo with some information. But stuck with the error "Argument of type 'File' is not assignable to parameter of type 'string'."
I am using angular 6 as frontent and as a backend, I'm using WebApi with SQL server 2012.
Thanks in advance guys and hoping for a soon response.
image-upload.ponent.ts
imageUrl:String="";
fileToUpload:File=null;
handleImageChange(file: FileList){
this.fileToUpload = file.item(0);
var reader = new FileReader();
reader.onload=(event:any)=>{
this.imageUrl=event.target.result;
}
reader.readAsDataURL(this.fileToUpload);
}
uploadImage(imageData){
let name=imageData.name;
let number=imageData.number;
let price=imageData.price;
this.service.uploadImage(name,this.fileToUpload,number,price).subscribe(
data=>{
alert("successfully uploaded");
this.productForm.reset();
this.imageUrl="";
}
);
}
image-upload.service.ts
uploadImage(fileToUpload:File, imagename:string, num:string, price:string){
let formData:FormData = new FormData();
formData.append("file",fileToUpload,fileToUpload.name);
formData.append("Imagename",imagename);
formData.append("Number",num);
formData.append("Price",price);
return this.http.post(this.baseUrl+"UploadImage",formData,this.httpOptions);
}
Error:
Share Improve this question edited Dec 13, 2019 at 7:54 Tony 20.2k7 gold badges41 silver badges62 bronze badges asked May 17, 2019 at 9:40 Pashupati PariyarPashupati Pariyar 611 gold badge2 silver badges12 bronze badges2 Answers
Reset to default 2You have that error because you putting the wrong order params
this.service.uploadImage(name,this.fileToUpload,number,price).subscribe(
Change to
this.service.uploadImage(this.fileToUpload, name,number,price).subscribe(
Because your params is like this
uploadImage(fileToUpload:File, imagename:string, num:string, price:string){
as per the parameter you defined called same like fileToUpload,name,number,price likewise...
本文标签: javascriptArgument of type 39File39 is not assignable to parameter of type 39string39Stack Overflow
版权声明:本文标题:javascript - Argument of type 'File' is not assignable to parameter of type 'string' - Stack Ove 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742384618a2464779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论