The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
File Save javascript
By Guest on 24th January 2023 02:18:27 PM | Syntax: TEXT | Views: 14



New paste | Download | Show/Hide line no. | Copy text to clipboard
  1. function fixBinary (bin) {
  2.     var length = bin.length;
  3.     var buf = new ArrayBuffer(length);
  4.     var arr = new Uint8Array(buf);
  5.     for (var i = 0; i < length; i++) {
  6.       arr[i] = bin.charCodeAt(i);
  7.     }
  8.     return buf;
  9.   }
  10.  
  11.  
  12. // fileHandle is an instance of FileSystemFileHandle..
  13. async function writeFilePdf(contents, fileName) {
  14.     var options = {
  15.         startIn: 'downloads',
  16.         suggestedName: fileName,
  17.         types: [{
  18.             description: 'Pdf Document',
  19.             accept: {
  20.                 'application/pdf' : ['.pdf']
  21.             }
  22.         }]
  23.     };
  24.     var handle = await window.showSaveFilePicker(options);
  25.     // Create a FileSystemWritableFileStream to write to.
  26.     var writable = await handle.createWritable(); // Write the contents of the file to the stream.
  27.  
  28.     await writable.write(contents); // Close the file and write the contents to disk.
  29.  
  30.     await writable.close();
  31. }
  32.  
  33. async function writeFileZip(contents, fileName) {
  34.     var options = {
  35.         startIn: 'downloads',
  36.         suggestedName: fileName,
  37.         types: [{
  38.             description: 'Zip Document',
  39.             accept: {
  40.                 'application/zip' : ['.zip']
  41.             }
  42.         }]
  43.     };
  44.     var handle = await window.showSaveFilePicker(options);
  45.     // Create a FileSystemWritableFileStream to write to.
  46.     var writable = await handle.createWritable(); // Write the contents of the file to the stream.
  47.  
  48.     await writable.write(contents); // Close the file and write the contents to disk.
  49.  
  50.     await writable.close();
  51. }
  52.  
  53. function selectDownloadLocation(fileData, fileName, fileType) {
  54.     var contents = new Blob([fixBinary(atob(fileData))], { type: fileType });
  55.     fileType == '.pdf' ? writeFilePdf(contents, fileName) : writeFileZip(contents, fileName);
  56. }



  • Recent Pastes
  • Questions
    13 hours 57 mins 13 secs ago
  • Adrs
    14 hours 2 mins 23 secs ago
  • Docs
    14 hours 3 mins 7 secs ago
  • Servs
    14 hours 3 mins 50 secs ago
  • Srvs
    14 hours 8 mins 39 secs ago