"application/octet-stream" xlsx file

  • jubilant_courgette_93644-1308500004071149620

    Dov

    4 months ago

    Hi

    What is the best way to save and serve a spreadsheet that is being sent as a stream?

    Thanks for your help.
    1308500004511547443-image.png
    1308500005195088032-image.png
  • Tod-1308500008202408038

    Tod

    4 months ago

    Great energy @Dov! Your continuous contribution to the toddle Community just made you advance to Community Level 4!
  • jubilant_courgette_93644-1308553377793310762

    Dov

    4 months ago

    I managed to sort it. I converted the file to a base64 and sent that instead of a stream. For the future if anyone has this issue I used the following function
  • function handleExcelDownload(args) {
    try {
    // Parse the JSON string if necessary
    const data = typeof args.responseData === "string" ? JSON.parse(args.responseData) : args.responseData;

    // Extract the base64-encoded file content
    if (!data.file) {
    throw new Error("Invalid response format: Missing 'file' property");
    }

    const base64Data = data.file;

    // Decode the base64 string to binary
    const byteCharacters = atob(base64Data);
    const byteArrays = [];

    // Convert the binary string into an array of bytes
    for (let offset = 0; offset < byteCharacters.length; offset += 512) {
    const slice = byteCharacters.slice(offset, offset + 512);
    const byteNumbers = new Array(slice.length);
    for (let i = 0; i < slice.length; i++) {
    byteNumbers[i] = slice.charCodeAt(i);
    }
    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
    }

    // Create a Blob from the byte arrays
    const blob = new Blob(byteArrays, { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });

    // Create a temporary link element and trigger the download
    const link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = 'customer-rates.xlsx';
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(link.href);
    } catch (error) {
    console.error('Failed to download the file:', error);
    }
    }

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code — but accessible to your entire team!

Try toddle — it's free!

© Copyright 2024 toddle. All rights reserved.