Download from Slate - slate.downloadBlob

Trying to use theslate.downloadBlobfunctionality

I have this code attached to a button::onClick, (it is just creating a hello world text file) from the documentation. but no file download is triggered.

// Debug log: selected row keys (paths)
console.log("Selected row keys (paths):", {{w_table_1.selectedRowKeys}});

const selectedRowKeys = {{w_table_1.selectedRowKeys}};
const tableData = {{w_table_1.data}};
const EW_RAW_FILES_RID = "ri.foundry.main.dataset2d371154-548e-4b6f-b20c-44a1cf650928";


if (selectedRowKeys.length > 0) {
  const fileName = selectedRowKeys[0]; 

  // Debug log: file to be downloaded
  console.log("Downloading file:", fileName);

    try {       
          var theQuery =  {{q_query_1}}; 

          console.log("File downloaded successfully:", fileName);
          console.log("Query result:", theQuery.result);
          console.log("Query result:", theQuery.result[0]);

           // Handle different result formats
          let fileData;
          //let mimeType = "application/octet-stream"; // Default for binary files
          let mimeType = "application/pdf"; // Default for binary files

          
          if (theQuery.result && theQuery.result.length > 0) {
            fileData = theQuery.result[0];
            
            // If it's a string (base64), convert it
            if (typeof fileData === 'string') {
              // Assuming base64 encoded data
              const binaryString = atob(fileData);
              const bytes = new Uint8Array(binaryString.length);
              for (let i = 0; i < binaryString.length; i++) {
                bytes[i] = binaryString.charCodeAt(i);
              }
              fileData = bytes;
            }
          } else {
            throw new Error("No file data received from query");
          }


          console.log("Download initiated for:", fileName);
          console.log("mimeType:", mimeType);


          let theBlob =  new Blob(["Hello, world!"], { type: "text/plain;charset=utf-8" })
          //new Blob([fileData], { type: mimeType })
          console.log("Blob object:", theBlob);
          console.log("Blob size:", theBlob.size);
          console.log("Blob type:", theBlob.type);
                    
          //return {
          //  fileName: "yo.pdf",
          //  blob: theBlob
          //};

          return {
              fileName: "my_file.txt",
              blob: theBlob
          };
          
    
  } catch (error) {
    console.error("Error downloading file:", error);
    return null;
  }
}

// Debug log: no rows selected
console.log("No rows selected for download.");
return null;

Need to use the slate.downloadBlob ACTION on the button

Hello @oluies

from your last message I suspect you fixed the issue, but please let us know if using the slate.downloadBlob action did not work for you and will be happy to assist.

Best

Damian

Does not seem to be able to download anything if it is embedded as an iframe. I hope something similar will come to workshop - a generic download button with server side security

Hello

Do you mean that when your Slate app is accessed directly one can download a file, but it breaks when same Slate app is embedded inside an iframe? Are there any errors you get from the browser? I just tried such setup myself and Slate embedded in Workshop via Iframe Widget worked for me for file download - curious what yours is so I can try advising a solution!

Best

Damian