jibon57 / nativescript-mediafilepicker

A complete file picker solution for NativeScript
Apache License 2.0
51 stars 39 forks source link

File selected size 0 - IOS #56

Closed j99ht closed 5 years ago

j99ht commented 5 years ago

When a file is selected and the application tries to read the content, file size is 0

This is my code

openFilePicker() {
        let extensions = [];
        let that=this;

        if (app.ios) {
            extensions = [kUTTypeItem, kUTTypeText,kUTTypeArchive]; //you can get more types from here: https://developer.apple.com/documentation/mobilecoreservices/uttype
        } else {
            extensions = ['ctfw', 'ctdp','hex'];
        }

        let options: FilePickerOptions = {
            android: {
                extensions: extensions,
                maxNumberFiles: 2
            },
            ios: {
                extensions: extensions,
                multipleSelection: true
            }
        };

        let mediafilepicker = new Mediafilepicker(); 
        mediafilepicker.openFilePicker(options);

      mediafilepicker.on("getFiles", function (res) {
            let results = res.object.get('results');
            let fileName = results[0].file.replace(/^.*[\/]/, '');

            if (app.ios) {

                let folder = knownFolders.documents().getFolder("filepicker");
                let array=[];

                let file = folder.getFile(fileName);
                console.log("file size ", file.size);
          }

file.size is always 0

What I do is to download a file (text file with extension .hex) using Firefox and I want to use that file in my app, reading its content. I find Firefox downloads folder, I select the file, but the file looks empty.

I'm using Nativescript 5.1.0, tns-ios 5.1.1 and "version": "2.0.13" of the plugin

jibon57 commented 5 years ago

Hi,

If you are using filepicker from iOS then you don't need to search that file under filepicker directory. You should get direct link. Try console.dir(results). Check demo project

j99ht commented 5 years ago

This is what I get if I remove filepicker

file:///private/var/mobile/Containers/Data/Application/9F99E06F-C3B0-44D6-B1FA-23A0B8F2528F/tmp/org.nativescript.myapp-Inbox/myfile.hex"

File path /var/mobile/Containers/Data/Application/9F99E06F-C3B0-44D6-B1FA-23A0B8F2528F/Documents/myfile.hex

And file size still 0

jibon57 commented 5 years ago

hmm.. not sure.. you can search more about UIDocumentPickerViewController

jibon57 commented 5 years ago

I think the way you are trying to get the size of file is wrong. Did you try to upload file in server after pickup? From Simulator the file is copying to Documents directory of your APP & from device it's tmp. So I think the correct way will be:

let folder = knownFolders.documents();
let file = folder.getFile(fileName);
....

Ref: https://docs.nativescript.org/ns-framework-modules/file-system#paths

j99ht commented 5 years ago

Yes, I did also in that way and I tried to read it but the file is empty

jibon57 commented 5 years ago

After download file, was you able to open/read it by using any other APP?

jibon57 commented 5 years ago

You can have a look here: https://stackoverflow.com/a/48007752/1281864 Try to copy file in some other place after pickup & see how it work.

nericode commented 5 years ago

I currently use this to get the video size:

var defManager = NSFileManager.defaultManager;
            var fileAttributes = defManager.attributesOfItemAtPathError(
                path
            );

            var fileSizeNumber = fileAttributes.objectForKey(
                NSFileSize
            );

            var fileSizeNumberB = fileSizeNumber / 1000;
            this.size = fileSizeNumberB;

But the path not support, any idea ?

j99ht commented 5 years ago

I tried to copy but no success. The file is there but empty.

paul-castro commented 4 years ago

tested on IOS:

import { File } from "tns-core-modules/file-system";

let results = res.object.get('results'); let path = results[0].file.split('://').pop(); let fileData = File.fromPath(path); console.log(fileData.size);

erkanarslan commented 4 years ago

@paul-castro's solution works. File paths are prefixed with file:// and also URI encoded. You need to remove that part and decode the URI to access files. This is my code to get correct path strings:

paths = paths.map(p => decodeURI(p.replace('file://', '')));
sudiptosen commented 4 years ago

@paul-castro, @erkanarslan thanks for the solution. I wish the File system threw a more straight forward error