|
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 | 11 | import mimeTypes from "mime";
|
12 | 12 |
|
@@ -51,24 +51,31 @@ export async function DownloadFile(file: mendix.lib.MxObject, openWithOS: boolea
|
51 | 51 |
|
52 | 52 | const dirs = RNBlobUtil.fs.dirs;
|
53 | 53 | const fileName = file.get("Name") as string;
|
54 |
| - const mimeType = mimeTypes.getType(fileName); |
55 | 54 | const sanitizedFileName = sanitizeFileName(fileName);
|
56 | 55 | const baseDir = Platform.OS === "ios" ? dirs.DocumentDir : dirs.DownloadDir;
|
57 | 56 | const filePath = mx.data.getDocumentUrl(file.getGuid(), Number(file.get("changedDate")));
|
58 | 57 | let accessiblePath;
|
| 58 | + |
59 | 59 | if (Platform.OS === "ios") {
|
60 | 60 | accessiblePath = await getUniqueFilePath(baseDir, sanitizedFileName);
|
61 | 61 | await RNBlobUtil.fs.cp(filePath, accessiblePath);
|
62 | 62 | } else {
|
| 63 | + const tempPath = await getUniqueFilePath(baseDir, sanitizedFileName); |
| 64 | + const base64Data = await mx.readFileBlob(filePath); |
| 65 | + const base64Content = base64Data?.split(",")[1]; |
| 66 | + await RNBlobUtil.fs.createFile(tempPath, base64Content, "base64"); |
| 67 | + const mimeType = mimeTypes.getType(fileName) as Mediatype; |
63 | 68 | accessiblePath = await RNBlobUtil.MediaCollection.copyToMediaStore(
|
64 | 69 | {
|
65 | 70 | name: sanitizedFileName,
|
66 | 71 | mimeType: mimeType ?? "*",
|
67 | 72 | parentFolder: ""
|
68 | 73 | },
|
69 | 74 | "Download",
|
70 |
| - filePath |
| 75 | + tempPath |
71 | 76 | );
|
| 77 | + |
| 78 | + RNBlobUtil.fs.unlink(tempPath); |
72 | 79 | }
|
73 | 80 | if (openWithOS) {
|
74 | 81 | await FileViewer.open(accessiblePath, {
|
|
0 commit comments