appsmithorg / appsmith

Platform to build admin panels, internal tools, and dashboards. Integrates with 25+ databases and any API.
https://www.appsmith.com
Apache License 2.0
34.71k stars 3.75k forks source link

[Bug]: Unable to download octet stream using the built-in download function #30285

Open LagunaElectric opened 10 months ago

LagunaElectric commented 10 months ago

Is there an existing issue for this?

Description

A users API returns an image as a binary octet stream. When you try downloading the stream with the following code snippet you get a corrupted file.

async download() {
  let blob = new Blob([FilePicker1.files[0].data], {type: "application/octet-stream"})
  let image = URL.createObjectURL(blob)
  await download(image, 'sample.png', 'image/png')
}

Here I've asked the user for a sample output from their API and uploaded that as a file to the Filepicker widget. You can find the sample in the attached Zendesk thread.

Steps To Reproduce

  1. D&D a Filepicker widget and set the file type to binary.
  2. Upload the file provided by the user to a Filepicker widget. You may need to ensure the file is saved as binary before uploading. a. Note: Any octet stream should do. It doesn't have to be the users output.
  3. Add the above function to a JS Object and run it.
  4. Observe that the downloaded file will not open properly.

Environment

Production

ZD Thread

Ticket 1295

Version

Cloud 1.9.59-SNAPSHOT

arunvjn commented 10 months ago

Could you please try the following snippet instead ? @LagunaElectric

async downloadImage() {
  await download(FilePicker1.files[0].data, 'sample.png', 'image/png')
}
LagunaElectric commented 10 months ago

@arunvjn That gives the same result, downloaded file is corrupt and can't be opened.

ollcp commented 8 months ago

import external js library ky , thanks for sindresorhus

export default {

    DownloadBinary: async () => {

        let result = await ky.post("http://x.x.x.x/download",{});
        let buffer = await result.arrayBuffer();

        const blob = new Blob([buffer], {type: 'application/octet-stream'});
        const data = URL.createObjectURL(blob);
        await download(data, "sample.pdf","application/octet-stream")
    }

}

@LagunaElectric