代码之家  ›  专栏  ›  技术社区  ›  Suraj Libi

Ionic formData追加在服务器中显示空值

  •  1
  • Suraj Libi  · 技术社区  · 7 年前

    我正在尝试使用formData上载图像。api运行良好。但数据在服务器中显示为空。 我的职责是

      capture_dl_front(){
        this.camera.getPicture(this.cameraOptions)
          .then(imageData => {
            this.customer.dl_front = normalizeURL(imageData);
            this.upload_dl_front(imageData);
          }, error => {
            this.func.showAlert('Error',JSON.stringify(error));
          });
      }
      upload_dl_front(imageFileUri: any): void {
        this.file.resolveLocalFilesystemUrl(imageFileUri)
          .then(entry => (<FileEntry>entry).file(file => this.readFile_dl_front(file)))
          .catch(err => console.log('Error',JSON.stringify(err)));
      }
      private readFile_dl_front(file: any) {
        const reader = new FileReader();
        reader.onloadend = () => {
          const imgBlob = new Blob([reader.result], { type: file.type });
          this.dl_front_imageUri = imgBlob;
          this.dl_front_imageName = file.name;
          alert(this.dl_front_imageName)
          const img = new FormData();
          img.append('image', this.dl_front_imageUri, this.dl_front_imageName)
          this.api.test(img).then(data=>alert("final: "+data))
        };
        reader.readAsArrayBuffer(file);
      }
    

    我的api函数是

     test(image){
        let headers = new HttpHeaders({
          'Content-Type': 'application/x-www-form-urlencoded',
        });
        return new Promise( resolve => {
          this.http.post(url, image, { headers: headers})
            .subscribe(
              data => {
                resolve(data['message']);
              },
              error => {
                resolve(error.statusText);
              }
            );
        });
      }
    

    我把文件放在我的服务器上

    $image = $request->file('image');
    

    但图像参数中的值为空。

    我在这里做错什么了?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sujan Gainju    7 年前

    您应该删除api调用中的头。

     test(image){
        return new Promise( resolve => {
          this.http.post(url, image)
            .subscribe(
              data => {
                resolve(data['message']);
              },
              error => {
                resolve(error.statusText);
              }
            );
        });
      }