Angular 我试图上传图像使用角度,但得到错误

Angular 我试图上传图像使用角度,但得到错误,angular,typescript,Angular,Typescript,上传图像时出错,服务器抛出错误无法读取未定义的属性“buffer”,我使用nodejs作为后端服务器,但当我通过邮递员发送图像时,图像成功存储在mongodb中 我已经经历了各种关于堆栈溢出的帖子,但仍然得到错误,我不知道我错过了什么 //html代码 <label for="InputImage">Upload Image</label> <input type="file" accept="image/*" title="image" (change)="ha

上传图像时出错,服务器抛出错误
无法读取未定义的属性“buffer”
,我使用nodejs作为后端服务器,但当我通过邮递员发送图像时,图像成功存储在mongodb中

我已经经历了各种关于堆栈溢出的帖子,但仍然得到错误,我不知道我错过了什么

//html代码

<label for="InputImage">Upload Image</label>
<input type="file" accept="image/*" title="image" 
(change)="handleImageInput($event)"
 class="form-control" id="InputImage">
//服务台

addPackage(packageData: IProduct,imageData) {
console.log(packageData);
return this.http.post(this.apiUrl + 'spectrum/package/addPackage', 
{
  title: packageData.title,
  productNames: packageData.productNames,
  productQuantities: packageData.productQuantities,
  price: packageData.price
},imageData)
}

您使用FormData对象正确地构造图像数据,但不应使用HttpClient的第三个参数发送图像数据。使用FormData发送数据时,应使用FormData对象发送所有数据。在服务器端,您还需要在端点上指定请求内容类型为“multipart/data form”,不能在一个端点上混合使用“application/json”和“multipart/data form”。你应该只使用其中一个

我想你需要添加标题,试试这个

addPackage(packageData: IProduct,imageData) {
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');            
headers.append('Accept', 'application/json');
    let options = new RequestOptions({ headers: headers });
    console.log(packageData);
    return this.http.post(this.apiUrl + 'spectrum/package/addPackage', 
    {
      title: packageData.title,
      productNames: packageData.productNames,
      productQuantities: packageData.productQuantities,
      price: packageData.price
    },imageData, options)
    }

希望有用。

感谢joep kockelkorn对您的贡献感谢Tanks khalifa对您的贡献它现在正在工作
addPackage(packageData: IProduct,imageData) {
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');            
headers.append('Accept', 'application/json');
    let options = new RequestOptions({ headers: headers });
    console.log(packageData);
    return this.http.post(this.apiUrl + 'spectrum/package/addPackage', 
    {
      title: packageData.title,
      productNames: packageData.productNames,
      productQuantities: packageData.productQuantities,
      price: packageData.price
    },imageData, options)
    }