服务器节点JS文件上传multer:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, callBack) {
console.log(req.body);
callBack(null, 'public/images/');
}, filename: function (req, file, callBack) {
callBack(null, 'someName');
}
});
var upload = multer({ storage: storage }).single('file');
outer.post('/uploads',uploads.single('file'),
function(req, res) {
//console.log(JSON.stringify(req.files.file));
console.log('/////////////////////////////////');
console.log(JSON.stringify(req.body));
console.log(JSON.stringify(req.params))
console.log(req.files.file)
var base64Data = req.files.file.data.toString('base64').replace(/^data:image\/jpeg;base64,/, "");
//console.log(base64Data);
var time = Date.now().toString()+"out.jpeg" ;
fs.writeFile('public/images'+"/"+time, base64Data, 'base64', function(err) {
console.log(err +' §§§§§§§§§§§' );
});
res.status(204).end();
}
);
前端离子3:
var name = "upload";
var par = {
token : this.myToken ,
contenu_titre : this.contenu_titre ,
contenu_text : this.contenu_text ,
contenu_type : 'img' ,
} ;
let option: FileUploadOptions = {
params : par,
fileKey:'file',
mimeType:'image/jpeg',
httpMethod:'POST',
fileName:'user_step4#'+name
};
let loader = this.loadingCtrl.create({
content: "Uploading...."
});
loader.present();
const fileTransfer:FileTransferObject = this.transfer.create();
// console.log('filename'+this.curfilename);
fileTransfer.upload(this.photo ,encodeURI(this.linkPic+"/publication/uploads"),option).then((result)=>
{
alert('uploaded')
console.log('success');
console.log(result);
loader.dismiss();
}).catch(error=>{
loader.dismiss();
alert(error);
console.log('uploaderror');
console.log(error.message);
});
}
我想在服务器中获取Params值, 每次未定义或为null 我想获取一些参数,并将文件保存在数据库中。 上载正常,但无法获取参数 req.body return {} 并且req.file返回文件名和文件
答案 0 :(得分:0)
您可以将 params与FileTransferOptions 一起使用,以将参数与文件一起发送。 使用mimeType:“ multipart / form-data” 指定该请求包含多种数据类型
var options =
{
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params: {
"full_name": this.userForm.value.u_name,
"email": this.userForm.value.u_email,
"country": this.userForm.value.u_country,
"state": this.userForm.value.u_state,
"city": this.userForm.value.u_city,
"pincode": this.userForm.value.u_pincode,
"fax": this.userForm.value.u_fax,
"address": this.userForm.value.u_address,
}
};
transfer.upload(this.photo, this.yourURL, options).then(data =>
{
//Handle the response from the server
}