[英]Argument of type 'File' is not assignable to parameter of type 'string'
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 .net WebApi with SQL server 2012.我使用 angular 6 作为前端和后端,我使用 .net WebApi 和 SQL server 2012。
Thanks in advance guys and hoping for a soon response.在此先感谢大家,并希望尽快回复。
image-upload.component.ts图片-upload.component.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图片-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:错误:
You 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){
根据您定义的参数,如 fileToUpload,name,number,price 一样...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.