Я пытаюсь загрузить файл на сервер.В настоящее время я загружаю изображения. Это мой клиентский код
onClickfileupload(){
let data={};
let path="";
let api_url = '/api/uploadFileFromIonicHome';
this.fileChooser.open()
.then(uri =>
{
path= JSON.stringify(uri);
alert(path);
data={
userEmail: this.userEmail,
userName: this.userName,
userRole: this.userRole,
fileComment: this.desc,
type: "file",
fullFileName:'file-'+Date.now()+'.jpg'
}
this.imagesProvider.uploadImage(path, data, api_url).then((res) => {
let dataPassedBackObj = {
reload: true,
pathOfFile: path,
typeOf: "picture",
userName: this.userName,
fileComment: this.desc
}
alert("Successfully uploaded picture...");
this.events.publish('toggleMenu');
}, err => {
alert(err.http_status);
alert("There was error in uploading picture...");
}); })
.catch(e => alert(e));
}
Это мой провайдер, который я использую для загрузки файла на серверную часть. Это код моего провайдера
uploadImage(img, data ,api_url) {
alert("Uploading file...");
// Destination URL
let url = SERVER_HOST + api_url;
// File for Upload
var targetPath = img;
console.log("line 28"+targetPath);
var options: FileUploadOptions = {
fileKey: 'image',
chunkedMode: false,
mimeType: 'multipart/form-data',
params: data,
};
const fileTransfer: FileTransferObject = this.transfer.create();
return fileTransfer.upload(targetPath, url, options);
}
Это код моего сервера, где я получаю маршруты.но требуетсятело пустеет {}. Это часть моего серверного кода
app.post('/api/uploadFileFromIonicHome', uploadFromHome.single('image'), function(req, res) {
console.log('within /api/uploadFileFromIonicHome');
console.log(req.body.type);
console.log(req.body);
var userName = req.body.userName;
var userEmail = req.body.userEmail;
var userType = req.body.userRole;
var fileName = req.body.fileName;
var type = req.body.type;
var comment = req.body.comment;
var fileComment = req.body.fileComment;
Я получаю пустое "req.body" и "req.body.type не определено". Это ошибка, которую я получаю typeerror: невозможно преобразовать объект в примитивное значение