如何在ionic3中的插件文件传输中发送表单数据参数?

时间:2019-01-21 07:52:30

标签: ionic3

我正在尝试在ionic3上上传图像,并且正在使用@ ionic-native / file-transfer和

public uploadImage() {
  // Destination URL
  var url = "https://linker.co.in/auth/update_profile_image";
 
  // File for Upload
  var targetPath = this.pathForImage(this.lastImage);
  // File name only
  var filename = this.lastImage;

   //
  var clientConfirmData = new FormData();
  clientConfirmData.append('image', filename);

    var options = {
      fileKey: "file",
       chunkedMode: false,
      fileName: this.lastImage,
      mimeType: "multipart/form-data",
       headers: {'userid' :  this.userObject.user_id},
      params : clientConfirmData
    };
 
    const fileTransfer : FileTransferObject = this.transfer.create();
  
  // Use the FileTransfer to upload the image
  fileTransfer.upload(targetPath, url, options).then(data => {
      let response = data.response;
      var r = JSON.parse(response);

      if(r.status) { 
        this.selectedImage = r;
        this.userObject.image = r.image;
      } 

    this.presentToast('Image succesful uploaded');
  }, err => {
    console.log('ddd' +err);
    this.presentToast('Error while uploading file.');
  });
}

我已经以formData发送了图像参数,并且我正在使用以下格式:

  

var clientConfirmData = new FormData();     clientConfirmData.append('image',filename);

但是它不起作用,我每次都从Backend得到零响应。请告诉我,任何人,如何以我尝试过的上述格式的formData发送参数,但是它不起作用。

1 个答案:

答案 0 :(得分:0)

文件传输已被弃用我没有运气才能正常工作。我在离子项目中使用了有角度的httpclient,所以就像这样

let options = new HttpHeaders().set('Content-Type', 'application/json');
body = {
'image': filename
}
this.http.post("https://ams.consagous.co.in/auth/update_profile_image",
 body, {
  headers: options
}).subscribe(response => { 
  return console.log('response');
});