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 badges
Add a ment  | 

2 Answers 2

Reset to default 2

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){

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