在以下代码中,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.');
});
}
答案 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
一样。