I am trying to upload files by using help of ionic/file-chooser plugin. Everything was perfect until i did not get requirement to upload a doc or pdf file.
I am using ionic 3.12.0 and cordova 7.0.1.
` const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'files0',
fileName: 'name.jpg',
params: {
Authkey: this.CS.auth_key,
Token: this.CS.token,
group_id: this.CS.activeGroupId,
Userid: this.CS.userId
}
};
this.fileChooser.open()
.then(uri => {
this.CS.show_loader();
alert(uri );
this.file.resolveLocalFilesystemUrl(uri)
.then((files)=> {
alert(JSON.stringify(files, null, 4));
}).catch((err) => {
alert(JSON.stringify(err, null, 4));
});
let name = uri.split("/");
options.fileName = name[name.length - 1];
alert(options.fileName);
fileTransfer.upload(uri, this.CS.base_url + 'groups/uploadChat', options)
.then((data) => {
this.CS.hide_loader();
this.res = data;
alert(JSON.stringify(data, null, 4));
let d = JSON.parse(this.res.response);
if(d.status == 1){
let a = {
member_id: this.user_id,
imageLink: this.groupChatData.loginUserData.imageLink,
message: '',
attachment: d.data[0].attachment,
member_name: this.CS.userData.name,
mime_type: d.data[0].mime_type,
attachment_imageUrl: d.data[0].attachment_imageUrl,
attachment_linkUrl: d.data[0].attachment_linkUrl,
send_at: Date.now()
};
this.group_chat.push(a);
setTimeout(() => this.content.scrollToBottom(), 200);
}else{
this.CS.alertMessage("error", this.res.response.message);
}
}, (err) => {
alert(JSON.stringify(err, null, 4));
})
})
.catch(e => alert(JSON.stringify(e, null, 4))); `
the resolveLocalFilesystemUrl return something like this -

There are neither file name nor file type, then how can i send file name to server?
@jitu-git This is not really an issue, but here's a very quick way to do it.
If this.file is an instance of File from @ionic-native/file:
const filePath = '<YOUR_FILE_PATH>';
const fileInfo = {};
this.file.resolveLocalFilesystemUrl(filePath)
.then((entry: FileEntry) => {
return new Promise((resolve, reject) => {
entry.file(meta => resolve(meta), error => reject(error));
});
})
.then((meta: IFile) => {
fileInfo.name = meta.name;
fileInfo.type = meta.type; // This is a value compatible with the 'Content-Type' HTTP header
fileInfo.size = meta.size;
return fileInfo;
})
There are other ways to "derive" this information but you might as well let the OS do it for you.
Now resolveLocalFilesystemUrl returns Entry not FileEntry.
The above does not work anymore, the correct answer for this problem can be found here:
hi i wanna pass dynamic filelName and i changed accordingly, but it not working.
```
this.temp_image_name = new Date().getTime()+'.jpg';
this.temp_image_name = new Date().getTime()+'.jpg';
let options: FileUploadOptions = {
fileKey: 'file',
fileName: this.temp_image_name,
headers: {}
.....
}
```
any idea how to pass dynamic name ?
fileInfo.name = meta.name;
fileInfo.type = meta.type; // This is a value compatible with the 'Content-Type' HTTP header
fileInfo.size = meta.size;
bravo !!
Now
resolveLocalFilesystemUrlreturnsEntrynotFileEntry.
It still works for me.
` const filePath = this.filee.dataDirectory + '/file1.mp3';
const fileInfo: any = {};
this.filee.resolveLocalFilesystemUrl(filePath)
.then((entry: any) => {
return new Promise((resolve, reject) => {
entry.file(meta => resolve(meta), error => reject(error));
});
})
.then((meta: IFile) => {
fileInfo.type = meta.type; // This is a value compatible with the 'Content-Type' HTTP header
fileInfo.size = meta.size;
return fileInfo;
});`
Most helpful comment
@jitu-git This is not really an issue, but here's a very quick way to do it.
If
this.fileis an instance ofFilefrom@ionic-native/file:There are other ways to "derive" this information but you might as well let the OS do it for you.