2017-03-04 85 views
1

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

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.'); 
    }); 
} 

回答

3

您可以在options

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

user_id将收到POST就像filenameparams财产包括这样的数据。

+0

很酷,非常感谢你@suraj –

+0

我传递参数中的关联数组,它不可访问,所以我们如何传递关联数组? –