使用额外数据上传ionic2文件

时间:2017-03-04 13:03:29

标签: cordova angular file-upload ionic-framework ionic2

在以下代码中,fileTransfer.upload()方法将文件上载到远程服务器。但是我们如何发送额外的数据,例如谁上传,如发送用户ID。

public uploadImage() {
  // Destination URL
  var url = "http://yoururl/upload.php";

  // File for Upload
  var targetPath = this.pathForImage(this.lastImage);

  // File name only
  var filename = this.lastImage;

  var options = {
    fileKey: "file",
    fileName: filename,
    chunkedMode: false,
    mimeType: "multipart/form-data",
    params : {'fileName': filename}
  };

  const fileTransfer = new Transfer();

  this.loading = this.loadingCtrl.create({
    content: 'Uploading...',
  });
  this.loading.present();

  // Use the FileTransfer to upload the image
  fileTransfer.upload(targetPath, url, options).then(data => {
    this.loading.dismissAll()
    this.presentToast('Image succesful uploaded.');
  }, err => {
    this.loading.dismissAll()
    this.presentToast('Error while uploading file.');
  });
}

1 个答案:

答案 0 :(得分:6)

您可以在params

options属性中包含此类数据
var options = {
    fileKey: "file",
    fileName: filename,
    chunkedMode: false,
    mimeType: "multipart/form-data",
    params : {
      'fileName': filename,
      'user_id': userId
    }
  };

user_id将作为POST收到,就像filename一样。