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

时间:2019-05-17 09:40:45

标签: angular

我正在尝试上传包含一些信息的照片。但是卡在错误中,“文件类型的参数不能分配给字符串类型的参数。”

我将angular 6作为前端和后端,并将SQL Server 2012与.net WebApi结合使用。

先谢谢大家,希望很快能收到答复。

image-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

 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);  
  }

错误:

enter image description here

2 个答案:

答案 0 :(得分:1)

您有该错误,因为您下错了顺序  

this.service.uploadImage(name,this.fileToUpload,number,price).subscribe(

更改为

this.service.uploadImage(this.fileToUpload, name,number,price).subscribe(

因为您的参数就是这样

uploadImage(fileToUpload:File, imagename:string, num:string, price:string){

答案 1 :(得分:0)

按照您定义的参数调用相同的文件,例如fileToUpload,名称,编号,价格...