我正在使用文件传输插件将文件上传到服务器。在这里我也想发送其他参数。
下面是代码。
public uploadVideo(path:any,name:any,filetype:any){
const fileTransfer: FileTransferObject = this.transfer.create();
let options1: FileUploadOptions = {
fileKey: 'video_upload_file',
fileName: name,
headers: {},
mimeType: 'mp4/webm',
params: {"storage":filetype},
chunkedMode: false
};
fileTransfer.upload(path, 'http://forcast.co.in:8888/api/v1/uploadVideo', options1)
.then((data) => {
const alert = this.alertCtrl.create({
title: 'Success',
subTitle: 'data saved successfully',
buttons: ['OK']
});
alert.present();
}, (err) => {
const alert = this.alertCtrl.create({
title: 'Error',
subTitle:JSON.stringify(err),
buttons: ['OK']
});
alert.present();
})
}
以上代码将文件上传到服务器,但是 params:{“ storage”:filetype}, 在这里,我在类型为options的参数中传递了一个键值对 FileUploadOptions 。
如何在服务器端访问或显示此键值。 我在服务器端使用节点js。 下面是服务器端代码。
public uploadVideo(req: any, res: any) {
if (!req.files) {
console.log('No files uploaded');
return res.status(400).send('No files were uploaded.');
}
let document = req.files.video_upload_file;
console.log(document.name);
console.log(document,"Checking for all params")
let docfiles = req.files;
console.log(docfiles,"all parameters in uploadvideo")
let docfiletype = req.files.params;
console.log(docfiletype,"parameters in uploadvideo")
const uploadDir = '/home/forcast/forcast2443/media-repo/';
if (!fs.existsSync(uploadDir)) {
fs.mkdirSync(uploadDir);
}
document.mv(uploadDir + document.name, err => {
if (!err) {
console.log('Upload successful!');
res.status(200).send({
message: 'Upload successful!'
});
} else {
console.log('Error saving file: ' + err);
return res.status(500).send(err);
}
});
}
如何显示在选项内传递的参数值?如您所见,甚至我正在显示console.log(req.files),但这不包含params的值。
请帮助我