【问题标题】:Ionic 3 File-Transport multipart/form-dataIonic 3 文件传输多部分/表单数据
【发布时间】:2018-04-12 00:09:27
【问题描述】:

我实际上正在使用 angular5 开发 ionic v3 中的移动应用程序

目标是能够拍照或从现有照片中进行选择,然后将其上传到服务器。第一部分已经完成,但我正在努力上传。

api 需要 multipart/form-data 必须由两个请求组成。首先是文字部分,其次是图片。

有什么解决办法吗?

【问题讨论】:

    标签: http post ionic-framework multipartform-data file-transfer


    【解决方案1】:

    这是我为类似需求所做的

    takePhoto() {
        this.camera.getPicture({
          quality: 100,
          destinationType: this.camera.DestinationType.FILE_URI,
          sourceType: this.camera.PictureSourceType.CAMERA,
          encodingType: this.camera.EncodingType.PNG,
          saveToPhotoAlbum: true
        }).then(imageData => {
          this.myPhoto = imageData;
          this.uploadPhoto(imageData);
        }, error => {
          this.functions.showAlert("Error", JSON.stringify(error));
        });
      }
    
      selectPhoto(): void {
        this.camera.getPicture({
          sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
          destinationType: this.camera.DestinationType.FILE_URI,
          quality: 100,
          encodingType: this.camera.EncodingType.PNG,
        }).then(imageData => {
          this.myPhoto = imageData;
          this.uploadPhoto(imageData);
        }, error => {
          this.functions.showAlert("Error", JSON.stringify(error));
        });
      }
      private uploadPhoto(imageFileUri: any): void {
        this.file.resolveLocalFilesystemUrl(imageFileUri)
          .then(entry => (<FileEntry>entry).file(file => this.readFile(file)))
          .catch(err => console.log(err));
    
      }
      private readFile(file: any) {
        const reader = new FileReader();
        reader.onloadend = () => {
          const formData = new FormData();
          const imgBlob = new Blob([reader.result], { type: file.type });
          formData.append('evaluationID', this.currentEvaluation.evaluationId);
          formData.append('standardID', this.currentEvaluation.id);
          formData.append('score', this.currentEvaluation.score);
          formData.append('comment', this.currentEvaluation.comment);
          formData.append('file', imgBlob, file.name);
          this.saveStandard(formData);
        };
        reader.readAsArrayBuffer(file);
      }
    

    这里是提供者的代码

    saveStandard(receivedStandardInfo:any){
        return new Promise((resolve, reject) => {
          this.http.post(apiSaveStandard,receivedStandardInfo)
            .subscribe(res => {
              resolve(res);
            }, (err) => {
              console.log(err);
              reject(err);
            });
        }).catch(error => { console.log('caught', error.message); });
      }
    

    【讨论】:

    • 在我的情况下 render.result 返回空白。你能告诉我有什么问题吗?
    • 和reject(err)返回这个'caught Cannot read property'length' of undefined'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 2016-06-29
    • 1970-01-01
    • 2016-04-28
    • 2016-06-18
    相关资源
    最近更新 更多