-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUploadDialog.jsx
146 lines (131 loc) · 3.91 KB
/
UploadDialog.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useState } from "react";
import {
Button,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Typography,
IconButton,
Box,
} from "@mui/material";
import { CloudUpload } from "@mui/icons-material";
import CloseIcon from "@mui/icons-material/Close";
const UploadDialog = ({ open, onClose }) => {
const [files, setFiles] = useState([]);
const handleFileChange = (event) => {
setFiles(event.target.files);
};
const handleUpload = () => {
// Check if files were selected
if (files.length === 0) {
console.error("No files selected");
return;
}
// Prompt the user to choose a directory for saving files
window
.showDirectoryPicker()
.then(async (directoryHandle) => {
// Iterate over each selected file
for (const file of files) {
const reader = new FileReader();
// Create a new Promise to read the file content asynchronously
const fileContentPromise = new Promise((resolve, reject) => {
reader.onload = function () {
resolve(reader.result);
};
reader.onerror = function () {
reject(reader.error);
};
});
// Read the selected file as ArrayBuffer
reader.readAsArrayBuffer(file);
// Wait for the file content to be loaded
const fileContent = await fileContentPromise;
const fileName = file.name;
// Create a writable file in the chosen directory
directoryHandle
.getFileHandle(fileName, { create: true })
.then(async (fileHandle) => {
// Create a writable stream to write the file content
const writableStream = await fileHandle.createWritable();
const writer = writableStream.getWriter();
// Write the file content to the writable stream
await writer.write(new Uint8Array(fileContent));
// Close the writable stream
await writer.close();
console.log(
`File ${fileName} saved successfully at ${fileHandle.name}`
);
onClose();
})
.catch((error) => {
console.error(`Error saving file ${fileName}:`, error);
});
}
})
.catch((error) => {
console.error("Error choosing directory:", error);
});
};
return (
<Dialog
open={open}
onClose={onClose}
maxWidth="xs"
fullWidth
PaperProps={{
sx: {
borderRadius: 2,
boxShadow: 8,
backgroundColor: "#fff",
},
}}
>
<DialogTitle style={{ paddingBottom: 8 }}>
<Box style={{ display: "flex", alignItems: "center" }}>
<CloudUpload fontSize="large" color="primary" />
<Typography
variant="h6"
style={{ marginLeft: 8, fontSize: "1.25rem" }}
>
Upload Accounts
</Typography>
<IconButton
style={{ marginLeft: "auto" }}
onClick={onClose}
aria-label="close"
>
<CloseIcon />
</IconButton>
</Box>
</DialogTitle>
<DialogContent>
<Box
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
paddingTop: 10,
}}
>
<input
type="file"
onChange={handleFileChange}
multiple
style={{ marginTop: 16, width: "100%" }}
/>
</Box>
</DialogContent>
<DialogActions style={{ paddingTop: 0 }}>
<Button onClick={onClose} color="primary">
Cancel
</Button>
<Button onClick={handleUpload} color="primary" variant="contained">
Upload
</Button>
</DialogActions>
</Dialog>
);
};
export default UploadDialog;