Download and open file

SDK Version: 45
iOS 15

Hello,

I’ve written the following code to download and open a file from an URL:

    // Opens an attachment
    const openAttachment = async (attachment) => {
        try {
            // Download file if not already done
            const fileInfo = await FileSystem.getInfoAsync(FileSystem.cacheDirectory + "attachment.name")
            console.log(fileInfo)
            let uri
            if (!fileInfo.exists) {
                console.log('Downloading attachment')
                resp = await FileSystem.downloadAsync(
                    server.value + `/index.php/apps/deck/api/v1.0/boards/${route.params.boardId}/stacks/${route.params.stackId}/cards/${route.params.cardId}/attachments/${attachment.id}`,
                    FileSystem.cacheDirectory + attachment.name,
                    {
                        headers: {
                            'Authorization': token.value
                        },
                    },
                )
                console.log(resp)
                uri = await FileSystem.getContentUriAsync(resp.uri)

            } else {
                console.log('File already in cache')
                uri = await FileSystem.getContentUriAsync(fileInfo.uri)
            }
            console.log('opening file', uri)
            Linking.openURL(uri)
        } catch {
            Toast.show({
                type: 'error',
                text1: i18n.t('error'),
                text2: error.message,
            })
			console.log(error)
        }
    }

For some reason, the call to FileSystem.getContentUriAsync(resp.uri) doesn’t return a content: URI and the call to Linking.openURL(uri) fails miserably.

I can get the following logs, so I know the file is correctly downloaded (eg: size is correct):

  1. 1st call to the openAttachment function:
Object {
  "exists": false,
  "isDirectory": false,
}
Downloading attachment
Object {
  "headers": Object {
    "Cache-Control": "no-cache, no-store, must-revalidate",
    "Connection": "Upgrade, Keep-Alive",
    "Content-Disposition": "attachment; filename=\"20191024113921.pdf\"",
    "Content-Security-Policy": "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
    "Content-Type": "application/pdf",
    "Date": "Sun, 16 Oct 2022 21:04:45 GMT",
    "Expires": "Thu, 19 Nov 1981 08:52:00 GMT",
    "Feature-Policy": "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
    "Keep-Alive": "timeout=5, max=100",
    "Pragma": "no-cache",
    "Referrer-Policy": "no-referrer",
    "Server": "Apache",
    "Strict-Transport-Security": "max-age=15552000; includeSubDomains",
    "Transfer-Encoding": "Identity",
    "Upgrade": "h2,h2c",
    "X-Content-Type-Options": "nosniff",
    "X-Frame-Options": "SAMEORIGIN",
    "X-Permitted-Cross-Domain-Policies": "none",
    "X-Request-Id": "rolY6PdXCoi4VaQucBmE",
    "X-Robots-Tag": "none",
    "X-XSS-Protection": "1; mode=block",
  },
  "mimeType": "application/pdf",
  "status": 200,
  "uri": "file:///var/mobile/Containers/Data/Application/5C1CB402-5ED1-4E17-B907-46111AE3FB7C/Library/Caches/test.pdf",
}
opening file file:///var/mobile/Containers/Data/Application/5C1CB402-5ED1-4E17-B907-46111AE3FB7C/Library/Caches/test.pdf

[Unhandled promise rejection: Error: Unable to open URL: file:///var/mobile/Containers/Data/Application/5C1CB402-5ED1-4E17-B907-46111AE3FB7C/Library/Caches/test.pdf]
at node_modules/react-native/Libraries/BatchedBridge/NativeModules.js:106:50 in promiseMethodWrapper
at node_modules/react-native/Libraries/Linking/Linking.js:70:13 in openURL
at node_modules/expo-linking/build/Linking.js:289:7 in getInitialURL
at http://192.168.0.9:19000/index.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:224979:37 in openURL
at components/AttachmentPanel.js:215:27 in openAttachment
  1. Second call to the openAttachment function:
Object {
  "exists": true,
  "isDirectory": false,
  "modificationTime": 1665954286.4155092,
  "size": 1129120,
  "uri": "file:///var/mobile/Containers/Data/Application/5C1CB402-5ED1-4E17-B907-46111AE3FB7C/Library/Caches/test.pdf",
}
File already in cache
opening file file:///var/mobile/Containers/Data/Application/5C1CB402-5ED1-4E17-B907-46111AE3FB7C/Library/Caches/test.pdf

I’ve 2 pdf reader apps installed (“Acrobat” and “Pdf reader”). So, that shouldn’t be the problem.

Any idea what’s going wrong?

Cyrille

same with Expo 46 :frowning:

Hi!

I’ve seen comments elsewhere suggesting that this functionality does not work on iOS and that we should clarify that, as content URI’s seem to be an Android-specific concept. Are you able to open the file with Linking if you pass it resp.uri directly?

Yes, I’ve proposed a fix for the documentation here => Update filesystem.mdx by StCyr · Pull Request #19577 · expo/expo · GitHub

No, it doesn’t change anything. It’s like Linking doesn’t like the file:// scheme (ie: if I replace uri by this URL, Download and open file - #2 by stcyr, it opens the browser correctly)…
Yeah, that’s the issue: the file:// scheme isn’t supported:using something like shareddocuments:///var/mobile/Containers/Data/Application/C75CCE92-A8CB-4358-A96B-B6A474106A83/Library/Caches/test.pdf opens iOS’ file explorer but the document isn’t there. So, I’m not following the right path to open these documents… 'Will have to find anohter way

Thanks for the help,

Cyrille

pfff…

No luck with expo-web-browser (cannot open a local file).
No luck with Sharing (not supported on my iphone, iOS 16).

Do I have any other options?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.