使用额外数据上传ionic2文件

Sha*_*ain 2 file-upload cordova ionic-framework ionic2 angular

在以下代码中,该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.');
  });
}
Run Code Online (Sandbox Code Playgroud)

Sur*_*Rao 6

您可以将此类数据包含在params属性中options

var options = {
    fileKey: "file",
    fileName: filename,
    chunkedMode: false,
    mimeType: "multipart/form-data",
    params : {
      'fileName': filename,
      'user_id': userId
    }
  };
Run Code Online (Sandbox Code Playgroud)

user_id会收到的POST只是喜欢filename.