- function fixBinary (bin) {
- var length = bin.length;
- var buf = new ArrayBuffer(length);
- var arr = new Uint8Array(buf);
- for (var i = 0; i < length; i++) {
- arr[i] = bin.charCodeAt(i);
- }
- return buf;
- }
- // fileHandle is an instance of FileSystemFileHandle..
- async function writeFilePdf(contents, fileName) {
- var options = {
- startIn: 'downloads',
- suggestedName: fileName,
- types: [{
- description: 'Pdf Document',
- accept: {
- 'application/pdf' : ['.pdf']
- }
- }]
- };
- var handle = await window.showSaveFilePicker(options);
- // Create a FileSystemWritableFileStream to write to.
- var writable = await handle.createWritable(); // Write the contents of the file to the stream.
- await writable.write(contents); // Close the file and write the contents to disk.
- await writable.close();
- }
- async function writeFileZip(contents, fileName) {
- var options = {
- startIn: 'downloads',
- suggestedName: fileName,
- types: [{
- description: 'Zip Document',
- accept: {
- 'application/zip' : ['.zip']
- }
- }]
- };
- var handle = await window.showSaveFilePicker(options);
- // Create a FileSystemWritableFileStream to write to.
- var writable = await handle.createWritable(); // Write the contents of the file to the stream.
- await writable.write(contents); // Close the file and write the contents to disk.
- await writable.close();
- }
- function selectDownloadLocation(fileData, fileName, fileType) {
- var contents = new Blob([fixBinary(atob(fileData))], { type: fileType });
- fileType == '.pdf' ? writeFilePdf(contents, fileName) : writeFileZip(contents, fileName);
- }
