|
6 | 6 | // - the code between BEGIN EXTRA CODE and END EXTRA CODE |
7 | 7 | // Other code you write will be lost the next time you deploy the project. |
8 | 8 | import { Platform } from "react-native"; |
9 | | -import RNBlobUtil from "react-native-blob-util"; |
| 9 | +import RNBlobUtil, { Mediatype } from "react-native-blob-util"; |
10 | 10 | import FileViewer from "react-native-file-viewer"; |
| 11 | +import mimeTypes from "mime"; |
| 12 | + |
11 | 13 | // BEGIN EXTRA CODE |
12 | 14 | function formatPath(...pathArgs: string[]): string { |
13 | 15 | return pathArgs.filter(arg => !!arg).join("/"); |
@@ -52,14 +54,28 @@ export async function DownloadFile(file: mendix.lib.MxObject, openWithOS: boolea |
52 | 54 | const sanitizedFileName = sanitizeFileName(fileName); |
53 | 55 | const baseDir = Platform.OS === "ios" ? dirs.DocumentDir : dirs.DownloadDir; |
54 | 56 | const filePath = mx.data.getDocumentUrl(file.getGuid(), Number(file.get("changedDate"))); |
55 | | - const accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName); |
| 57 | + let accessiblePath; |
56 | 58 |
|
57 | 59 | if (Platform.OS === "ios") { |
| 60 | + accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName); |
58 | 61 | await RNBlobUtil.fs.cp(filePath, accessiblePath); |
59 | 62 | } else { |
| 63 | + const tempPath = await getUniqueFilePath(baseDir, sanitizedFileName); |
60 | 64 | const base64Data = await mx.readFileBlob(filePath); |
61 | 65 | const base64Content = base64Data?.split(",")[1]; |
62 | | - await RNBlobUtil.fs.createFile(accessiblePath, base64Content, "base64"); |
| 66 | + await RNBlobUtil.fs.createFile(tempPath, base64Content, "base64"); |
| 67 | + const mimeType = mimeTypes.getType(fileName) as Mediatype; |
| 68 | + accessiblePath = await RNBlobUtil.MediaCollection.copyToMediaStore( |
| 69 | + { |
| 70 | + name: sanitizedFileName, |
| 71 | + mimeType: mimeType ?? "*", |
| 72 | + parentFolder: "" |
| 73 | + }, |
| 74 | + "Download", |
| 75 | + tempPath |
| 76 | + ); |
| 77 | + |
| 78 | + RNBlobUtil.fs.unlink(tempPath); |
63 | 79 | } |
64 | 80 | if (openWithOS) { |
65 | 81 | await FileViewer.open(accessiblePath, { |
|
0 commit comments