2017-03-04 60 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

我傳遞參數中的關聯數組,它不可訪問,所以我們如何傳遞關聯數組? –