javascript prompt for download location

Solutions on MaxInterview for javascript prompt for download location by the best coders in the world

showing results for - "javascript prompt for download location"
Nele
07 Feb 2019
1// ...
2const blob = new Blob(/*...*/);
3// Use File System Access API
4saveFileToDisk(blob, 'Some-File.txt')
5
6async saveFileToDisk({blob, fileName}){
7      try {
8        const fileHandle = await self.showSaveFilePicker({
9          suggestedName: fileName,
10          types: [
11            {
12              description: "File",
13              // ...
14            },
15          ],
16        });
17        const writeFile = async (fileHandle, contents) => {
18          // Create a FileSystemWritableFileStream to write to.
19          const writable = await fileHandle.createWritable();
20          // Write the contents of the file to the stream.
21          await writable.write(contents);
22          // Close the file and write the contents to disk.
23          await writable.close();
24        };
25        // write file
26        writeFile(fileHandle, blob).then(() => console.log("FILE DOWNLOADED!!!"));
27      } catch (error) {
28        console.log(error);
29      }
30}
31