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