简体   繁体   English

“文件”类型的参数不能分配给“字符串”类型的参数

[英]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.

相关问题 “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' 日期类型的参数不能分配给字符串类型的参数 - Argument of type date is not assignable to parameter of type string '[string]' 类型的参数不可分配给 'U' 类型的参数 - Argument of type '[string]' is not assignable to parameter of type 'U' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' “T”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'T' is not assignable to parameter of type 'string' “RegExp”类型的参数不能分配给“string”类型的参数 - Argument of type 'RegExp' is not assignable to parameter of type 'string' “字符串”类型的参数不可分配给“日期”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'Date' type script 类型参数 '{ type: string[]; }' 不可分配给“BlobPropertyBag”类型的参数 - type script Argument of type '{ type: string[]; }' is not assignable to parameter of type 'BlobPropertyBag' 角4-string []类型的参数不能分配给'string'类型的参数 - Angular 4 - argument of type string[] is not assignable to type parameter of type 'string' 参数类型不能分配给类型的参数 - Argument type is not assignable to parameter of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM