I am unable to get extension of audio file when using expo-document-picker

expo-document-picker is getting the data correctly for all images and videos but when I pick a audio file it does not have it’s extension.

Data I get via file picker:
Object {
“name”: “AUD-20200911-WA0067”,
“size”: 228339,
“type”: “success”,
“uri”: “file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540calendar-app%252FCalendar-app/DocumentPicker/6198429c-5d08-46b5-a6c8-caf4c1e84d3d.”,
}

There’s no mention of extension for this audio file.

I am having the same exact problem. Did you find a fix for it?

1 Like

Nope. I am now thinking of ejecting and then trying a different library.

If I try this on my Android phone I get the full filename in the “name”. This includes the extension.

What platform are you running this on? Do you know if the original file has an extension?

I think a mime type in the response would be useful, though.

I am running this on my Expo Go app (Android)

I guess its related to large files. Large files’s response object does not have the extension at the end of uri

These two response objects below are the result of this code
let result = await DocumentPicker.getDocumentAsync({ type: "*/*", }); console.log(result);

This logs

Object { "name": "New recording 1.m4a", "size": 4127720, "type": "success", "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540kartikeyvaish%252FEasyShop/DocumentPicker/9330da0a-77d2-4833-8cea-f5e6933251fc.m4a", }

Object { "name": "Khaleesi", "size": 6879965, "type": "success", "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540kartikeyvaish%252FEasyShop/DocumentPicker/c4500feb-2514-40b4-896d-b7ff4dd8f77c.", }

As you can see there is no extension in uri in second object.
What is the main reason behind this?

Sorry, I don’t know why one of them would have an extension and the other would not. I doubt it would have something to do with the file size, though.

What do you get if you try the following snack?

By the way, why do you need the file’s extension?

I edited your snack a little bit just to ensure whether the file I am choosing has an extension or not

Edited Part :

{info.assets.map(({ id, mediaType, filename }) =>
    filename === 'You Know You Like It.m4a' ? (
           <Text key={id} style={styles.paragraph}>
              {filename}
           </Text>
    ) : null
)}

I’ve attached the screenshot below

As you can see it shows a Track with an extension .m4a

Now back to this command

let result = await DocumentPicker.getDocumentAsync({ type: "*/*" }); 
console.log(result);

This prints the following

Object {
  "name": "You Know You Like It",
  "size": 10335743,
  "type": "success",
  "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540kartikeyvaish%252FEasyShop/DocumentPicker/f4cbf718-882a-41c0-b318-5a6005d21312.",
}

As you can see the response object does not have an .m4a extension in its uri but the file when accessed from MediaLibrary in the snack you provided shows the name with extension.
Is this a bug in Expo Document Picker?
Also, now when I pick the file which I was choosing before in my previous messages. I get this response

Object {
  "name": "Khaleesi.m4a",
  "size": 6879965,
  "type": "success",
  "uri": "file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540kartikeyvaish%252FEasyShop/DocumentPicker/499f7b15-326b-4a50-a2a5-973a88c41f9b.m4a",
}

I don’t know why but some files shows up with an extension and some not.

I need the file extension as my aim is to Upload the file to my database and store it.
Right now I am using multer in the backend to get the formData with buffer and then I use that buffer to create the file and store it in the backend.

But before sending the request to the backend I need to prepare the form data as shown

let form = new FormData();
let Extension = result.name.split(".").pop()
let type = Extension === '.m4a' ? 'audio' : 'image or video as per condtions..'
form.append("PreviewFile", {
     name: result.name,
     uri: result.uri,
     type: type + "/" + Extension ,
});

So this is the reason I need the extension of the file…

I wonder if it has something to do with the length of the uri.

Your code to get the mime type based on the extension is not strictly correct. But OK, I see what you’re doing.

Yeah I know… Actually this is my first project related to Uploading/Downloading files so I am learning day by day and I’ll try to get a better way of uploading files. But as of now I need the extension for this.:sweat_smile:

1 Like

Any Update on this @wodin

@kartikeyvaish I am pretty sure this has something to do with maximum file/path/uri lengths, although I don’t know the details or where the limit is being enforced.

If you look at the uris in this thread you’ll see they are all almost the same length, although I don’t know why some are 143 and some are 146 characters long. The actual file names are UUIDs, so the only thing you really have control over would seem to be your Expo username and the app slug. So as a test, what happens if you use a slug like “a”? You’d save 7 characters.

@notbrent any idea where this apparent limit is coming from and whether it would be possible for expo-document-picker to return the extension and maybe mime type separately so that one doesn’t have to try to infer that from the possibly truncated filename?