Ionic-native: How to get file name and mime type before upload?

Created on 27 Oct 2017  路  6Comments  路  Source: ionic-team/ionic-native

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 -
screenshot_20171027-083106
There are neither file name nor file type, then how can i send file name to server?

Most helpful comment

@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.

All 6 comments

@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:

https://stackoverflow.com/questions/46967119/how-to-get-file-name-and-mime-type-before-upload-in-ionic2/53933987

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 resolveLocalFilesystemUrl returns Entry not FileEntry.

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;
  });`
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyleap picture kyleap  路  4Comments

ajcrites picture ajcrites  路  3Comments

mateo666 picture mateo666  路  3Comments

goleary picture goleary  路  3Comments

lycwed picture lycwed  路  4Comments