【问题标题】:Getting Formdata empty even after appending values即使在附加值之后也让 Formdata 为空
【发布时间】:2020-10-02 01:41:10
【问题描述】:

我一直在尝试通过我的 IONIC 4 应用程序中的本地 http POST 请求发送图像。我正在获取选定的图像和 blob,但是当我尝试将其附加到我的 formData 时,我的 formData 始终为空。

这是我的 ts 代码

takePicture(sourceType: PictureSourceType) {
    var options: CameraOptions = {
      quality: 100,
      sourceType: sourceType,
      saveToPhotoAlbum: false,
      correctOrientation: true
    };
    this.camera.getPicture(options).then(imagePath => {
      if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) { 
        this.file.resolveLocalFilesystemUrl(imagePath).then((entry: FileEntry) => {
          entry.file(file => {
            console.log(file);
            this.readFile(file);
          });
        });
      }
    });
  }

  readFile(file: any) {
    const reader = new FileReader();
    reader.onloadend = () => {
      const imgBlob = new Blob([reader.result], {
        type: file.type
      });
      const formData = new FormData();
      formData.append('file', imgBlob, file.name);
      console.log(formData) 
      console.log(imgBlob)     
      this.uploadImageData(formData)
    };
    reader.readAsArrayBuffer(file);
  }

  async uploadImageData(formData) {
    let feedbackData = {
      attachment: formData,
      feedback: 'test text'
    }
    this.http.post('http://abctest.rapidesk.in/api/feedback/', feedbackData, { 'Content-Type': 'multipart/form-data', 'Authorization': "Token" + " " + this.authToken })
      .then(data => {
        console.log(data);
      }).catch(err => {
        console.log(err)
      })
  }

我已经分享了我的控制台图像。

  1. 第一个控制台显示我的图像位置和数据
  2. 第二个控制台是我的 formData
  3. 第三个控制台是我的 imgBlob

【问题讨论】:

  • 你不能在for (var pair of formData.entries()) { console.log(pair[0]+ ', ' + pair[1]); }下面尝试这样的控制台表单数据
  • @pc_coder 尝试使用 formData.entries 它给出“文件,[对象文件]”
  • 你能写console.log( pair[0],pair[1])
  • @pc_coder 它给了我文件详细信息,如名称和 blob 值

标签: javascript angular ionic-framework ionic4 angular8


【解决方案1】:

您不需要将图像数据绑定到 FormData。请按照我的代码。这对我来说很好。它可能会帮助你。

  getFileFromCamera() {
this.translate.get(["FILE_ADDED_SUCCESSFULLY", "UNABLE_TO_TAKE_PHOTO"]).subscribe(
  (values) => {
    this.camera.getPicture({
      destinationType: this.camera.DestinationType.DATA_URL,
      targetWidth: 500,
      targetHeight: 500,
      quality: 100,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      correctOrientation: true
    }).then((data) => {
      const imageData = 'data:image/jpg;base64,' + data;
      this.AddDocumentForm.patchValue({'form_file': imageData}); // I am using reactive form group but you can directly add to the API body. 
    }, (err) => {
      console.log(err);
      this.csp.showToastMessage(2, values.UNABLE_TO_TAKE_PHOTO);
    })
  });
 }

saveImage() {
   this.api.post(endPoint, {image : this.AddDocumentForm.value.form_file).subscribe(()=>{
        console.log(res);
   });
 }

// API service 
 post(endpoint: string, body: any, reqOpts?: any) {
      return this.http.post(this.url + '?' + endpoint, body, reqOpts);
 }

【讨论】:

  • 当我尝试将数据发送到我的 api 时,它说“提交的数据不是文件。请检查表单上的编码类型”。
  • 你按照上面的操作了吗?你在发送哑剧类型const imageData = 'data:image/jpg;base64,' + data; 吗?检查一次。做console.log()
  • 是的,我这样做了:this.camera.getPicture({ destinationType: this.camera.DestinationType.DATA_URL, targetWidth: 500, targetHeight: 500, quality: 100, encodingType: this.camera.EncodingType.JPEG, mediaType: this.camera.MediaType.PICTURE, correctOrientation: true }).then((data) => { const imageData = 'data:image/jpg;base64,' + data; this.uploadImageData(imageData) })
  • console.log(imageData) 给了我一个以 data:image/jpg;base64,/9j***** 开头的大文本,而且 api 是用 Python Django 编写的
  • 我可以从你的 console.log() 中看到。这是正确的。您基本上是在发送 base64 图像数据。按照这个在Django中上传https://stackoverflow.com/a/39587386/9739044
猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-09
  • 2016-11-18
  • 2021-08-03
  • 1970-01-01
  • 2019-06-26
相关资源
最近更新 更多