在ionic 3中将http请求中的图像附加到节点服务器时出现错误413

Alpesh Trivedi

我在App上工作,它需要上传多达4张图片以及其他数据(图片是可选的)。当使用Ionic Native HTTP请求带有其他数据的多个图像上传到节点服务器(API在nodejs中创建)时,出现以下错误。

error:{"statusCode":413,"error":"Request Entity Too Large","message":"Payload content lenght greater than maximum allowed: 1048576"}

我已经搜索了该问题,但除了以外没有其他解决方案,但不知道如何在ionic 3应用程序中应用它。

这是我的代码。

openGallery(){
    if (
      this.thumb1 != "" &&
      this.thumb2 != "" &&
      this.thumb3 != "" &&
      this.thumb4 != ""
    ) {
         this.presentToast("You can not upload more than 4 images.");
         return;
    }
    var options = {
        sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
        encodingType: this.camera.EncodingType.JPEG,
        quality: 10,
        destinationType: this.camera.DestinationType.DATA_URL
       // destinationType: this.camera.DestinationType.FILE_URI
    };
    this.camera.getPicture(options).then(imageData => { 
        let base64File = "data:image/jpeg;base64," + imageData;
        if (this.Image_0 === null) {
          this.Image_0 = base64File;
        } else if (this.Image_1 === null) {
          this.Image_1 = base64File;
        } else if (this.Image_2 === null) {
          this.Image_2 = base64File;
        } else if (this.Image_3 === null) {
          this.Image_3 = base64File;
        }

    let base64Image = base64File;
    if (this.thumb1 == "") {
      this.thumb1 = base64Image;
      this.thumb1 = this.sanitizer.bypassSecurityTrustResourceUrl(
        this.thumb1
      );
    } else if (this.thumb2 == "") {
      this.thumb2 = base64Image;
      this.thumb2 = this.sanitizer.bypassSecurityTrustResourceUrl(
        this.thumb2
      );
    } else if (this.thumb3 == "") {
      this.thumb3 = base64Image;
      this.thumb3 = this.sanitizer.bypassSecurityTrustResourceUrl(
        this.thumb3
      );
    } else if (this.thumb4 == "") {
      this.thumb4 = base64Image;
      this.thumb4 = this.sanitizer.bypassSecurityTrustResourceUrl(
        this.thumb4
      );
    }
  }).catch(e => {
    console.log("Error while picking from gallery", e);
  });
}
postForm(){
   let loading = this.loadingCtrl.create({
    spinner: "bubbles"
  });
  loading.present();
   let myData: any = {
     Type: 1,
     CustomerId: this.CustomerId,
     Rating: Rating,
     Review: this.review.value.comment
   };
   if (this.Image_0 != null) {
    myData.Image_0 = this.Image_0;
  }
  if (this.Image_1 != null) {
    myData.Image_1 = this.Image_1;
  }
  if (this.Image_2 != null) {
    myData.Image_1 = this.Image_2;
  }
  if (this.Image_3 != null) {
    myData.Image_3 = this.Image_3;
  }
  this.http
    .post(
      global.apiUrl + "restaurants/" + this._id + "/reviews",
      myData,
      headers
    )
    .then(data => {
    })
    .catch(error => {
      loading.dismiss();
      alert(JSON.stringify(error, Object.getOwnPropertyNames(error)));
    });
}

最近几天以来,我一直在寻找解决方案,但找不到解决方案,因此,请在此处发布。

更新:我们正在使用Hapi服务器。

Alpesh Trivedi

我已经成功完成了图片上传。错误是我没有指定图像的高度和宽度。

const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.FILE_URI,
  mediaType: this.camera.MediaType.PICTURE,
  targetWidth: 250,
  targetHeight: 200,
  correctOrientation: true
};

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章