eligrey / FileSaver.js

An HTML5 saveAs() FileSaver implementation
https://eligrey.com/blog/saving-generated-files-on-the-client-side/
Other
21.58k stars 4.38k forks source link

Save xlsx file into a particular path #719

Closed kakoo16 closed 3 years ago

kakoo16 commented 3 years ago

Hello ,

I'm trying to save an xlsx file into a particular path . How could i achieve this ?

<script lang="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.6/xlsx.full.min.js"></script>
<script lang="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.0/FileSaver.min.js"></script> 

 // Button that i'm using to download my XLSX
<button onclick="downloadExcel() ; "  id="download" class="tab-group" > Download JETON</button>

// Script that i'm using to download my xlsx file
    <script> 
function s2ab(s) {
  let buf = new ArrayBuffer(s.length); //convert s to arrayBuffer
  let view = new Uint8Array(buf); //create uint8array as viewer
  for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xFF; //convert to octet
  return buf;
}

function refresh() {
  document.getElementById('Variable1').selectedIndex =0;
  document.getElementById('Variable2').selectedIndex =0;
}

function downloadExcel() {
  // notifications part  
  Swal.fire({
    title: 'Do you want to confirm your jeton configuration ?',
    icon: 'success',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes!'
  }).then((result) => {
    if (result.isConfirmed) {
       // generate excel part
       console.log('download excel');
       let wb = XLSX.utils.book_new();
       wb.Props = {
      Title: "Run_File",
      Subject: "Jeton",
      Author: "USER",
      CreatedDate: new Date()
    };
    wb.SheetNames.push("Run_File");
    const Variable1 = document.getElementById('Variable1').value;
    const Variable2 = document.getElementById('Variable2').value;
    const InputEmail1 = document.getElementById('InputEmail1').value;
    let ws_data = [['key', 'value'], ['Variable1', Variable1], ['type', 'excel']  ,['mail', InputEmail1] ,['Variable2', Variable2] ];
    let ws = XLSX.utils.aoa_to_sheet(ws_data);
    wb.Sheets["Run_File"] = ws;
    let wbout = XLSX.write(wb, {bookType:'xlsx',  type: 'binary'});

    saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), 'Run_File.xlsx'); 
    refresh() ; 
    } else if (
      result.dismiss === Swal.DismissReason.cancel
    ) {
      Swal.fire(
        'Abort download',
        'Download stopped!',
        'error'
      );
    }
  });

}

</script>
jimmywarting commented 3 years ago

You can't save a file to a particular path.

You may have some luck trying out the new File system access https://github.com/WICG/file-system-access/blob/ad05f5f8dc6398fa2a19453e497810168e4f3e58/SuggestedNameAndDir.md

if you need a adapter/polyfill then there is this: https://github.com/jimmywarting/native-file-system-adapter/