如何在离子2/3上传图像服务器端

Jay*_*ngh 5 node.js ios ionic-framework server ionic2

我正在使用Ionic 2构建应用程序.我需要从图库或相机拍摄照片并将此照片上传到我的服务器.我有这个代码打开画廊并拍照.

accessGallery() {
    this.camera.getPicture({
      sourceType: this.camera.PictureSourceType.SAVEDPHOTOALBUM,
      destinationType: this.camera.DestinationType.DATA_URL
    }).then((imageData) => {
      this.base64Image = 'data:image/jpeg;base64,' + imageData;
      this.uploadFile();
    }, (err) => {
      console.log(err); 
    });
  }
Run Code Online (Sandbox Code Playgroud)

将图像上传到服务器端

uploadFile() {

      let body = new FormData();
      body.append('images', this.base64Image);
      let headers = new Headers({
        'token': this.token,
        'sid': this.sid,
        'user': this.user,
        'to': this.to,
        'node': this.node,
        'type':'image' 

      });
      let options = new RequestOptions({ headers: headers });
      console.log("header ----" +JSON.stringify(headers));
      console.log("images data body----" +JSON.stringify(body));

      this.http.post(this.apiurl,body,options)
          .map(res => res.json())
          .subscribe(
              data => {
                console.log(data);
              },
              err => {
                console.log("ERROR!: ", err);
              }
          );
    }
Run Code Online (Sandbox Code Playgroud)

错误: - 字段值太长

Dav*_*d R 0

正如错误描述清楚地表明的那样,您似乎正在尝试将一个值传递给 mysql 表的字段之一,该字段小于您尝试传递的值。

尝试输出options数组并对照相应的数据库字段交叉检查其每个值,如果发现任何差异,请相应地修改相应的字段长度。

希望这可以帮助!