# Issue with Downloading Binary Files from Weweb Collection (CORS & Binary String Issue)

**URL:** <https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674>\
**Category:** How do I?\
**Created:** [February 19, 2025, 9:36pm UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674 "2025-02-19T21:36:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jankolros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jankolros/32/12612_2.png) [@jankolros](https://community.weweb.io/u/jankolros)\
**Post date:** [February 19, 2025, 9:36pm UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/1 "2025-02-19T21:36:53Z")

</div>

I am experiencing an issue when trying to **download a binary file (PDF) using a Weweb collection**. The API I am using correctly returns **binary data** , but it appears that Weweb **converts it to a string** , causing the file to become corrupted when downloaded.

Additionally, this file is **only available through an endpoint that requires authentication**. When trying to fetch the file via JavaScript **inside Weweb** , I run into **CORS issues**.

**Issue Description:**

- I am fetching a **binary file (PDF)** from an **API that requires authentication**.

- The API correctly returns **binary data** (tested in **Postman** , where the file downloads correctly).

- When I try to store the response in a **Weweb collection** , it appears that **binary data is converted into a string**.

- When I attempt to download the file from Weweb, it **becomes corrupted and cannot be opened**. I have tried many different ways to convert it back to binary, nothing works.

- **Fetching the file using fetch() in JavaScript directly (inside Weweb) results in a CORS issue.**

**What I Have Tried:**

- **Fetching the file in Postman** → ✅ Works fine (file downloads correctly).

- **Fetching the file using fetch() in JavaScript (outside Weweb)** → ❌ Blocked by **CORS issue**.

- **Fetching it through Weweb’s collection** → ❌ File gets **converted to a string** , leading to corruption.

- 
  1. **Tried converting the string back to binary in JavaScript** → ❌ No success.

My Questions:

1. How does Weweb handle binary file responses in collections? Does Weweb automatically convert binary data to a string? If so, is there a way to prevent this? How can I correctly store and retrieve binary files using Weweb’s collections?

2. Is there a recommended way to bypass the CORS issue when fetching this file from an authenticated API?\*\*

Any guidance on resolving this issue would be greatly appreciated. Thank you in advance for your help!

Jan, Chorizo Lab

_Below is an example of Javascript code that I use to covert string to binary (not succesful)_

```auto
const binaryData = collections[/* GET Attachment/file */ 'xxxxxxxxxxx']?.['d\ta']; // Adjust path as needed

console.log(binaryData);

if (binaryData) {
    // Convert text-based binary string back to proper binary
    function fixBinaryString(str) {
        const encoder = new TextEncoder();
        return encoder.encode(str); //
    }

    // Convert Weweb binary data to a proper PDF Blob
    const byteArray = fixBinaryString(binaryData);
    const pdfBlob = new Blob([byteArray], { type: "application/pdf" });

    // Create a URL for the Blob
    const pdfUrl = URL.createObjectURL(pdfBlob);

    // Create a hidden download link
    const downloadLink = document.createElement("a");
    downloadLink.href = pdfUrl;
    downloadLink.download = "document.pdf"; // Set file name

    // Append, trigger click, and remove link
    document.body.appendChild(downloadLink);
    downloadLink.click();
    document.body.removeChild(downloadLink);
} else {
    console.error("Binary file data not found in Weweb collection.");
}

```

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [February 20, 2025, 6:26am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/2 "2025-02-20T06:26:28Z")

</div>

You need to route this through a server.

---

<div class="post-metadata">

**Author:** ![jankolros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jankolros/32/12612_2.png) [@jankolros](https://community.weweb.io/u/jankolros)\
**Post date:** [February 20, 2025, 11:38am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/3 "2025-02-20T11:38:48Z")

</div>

I would actually prefer using the WeWeb collection, where I can use the proxy by WeWeb. I just don’t understand what happens to the binary file. Any ideas?

Unfortunately, handling the CORS in the backend I am using is kinda complicated, so I leave it as a second option 🙂

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [February 20, 2025, 11:54am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/4 "2025-02-20T11:54:44Z")

</div>

What does the collection return? Can you post a sample? Maybe you need to set a proper header? How’s your collection’s setup? These are all of the unknowns we miss to be able to answer this.

---

<div class="post-metadata">

**Author:** ![jankolros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jankolros/32/12612_2.png) [@jankolros](https://community.weweb.io/u/jankolros)\
**Post date:** [February 25, 2025, 9:09pm UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/5 "2025-02-25T21:09:56Z")

</div>

Sorry, I cannot share the details…

But I made a workaround that solves the problem. Not sure if it is the best solution, but it works 🙂

Instead of fetching the file directly from WeWeb, I set up an n8n workflow that:  
• Starts with a Webhook node called from WeWeb  
• Fetches the file from the source API  
• Returns the binary file properly to WeWeb

Solved ✅

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [February 25, 2025, 9:47pm UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/6 "2025-02-25T21:47:02Z")

</div>

> [@Broberto](#):
>
> You need to route this through a server.

¯\_(ツ)_/¯

---

<div class="post-metadata">

**Author:** ![jankolros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jankolros/32/12612_2.png) [@jankolros](https://community.weweb.io/u/jankolros)\
**Post date:** [February 26, 2025, 8:20am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/7 "2025-02-26T08:20:34Z")

</div>

Sorry Broberto, that was not an answer that would lead me to solve the problem. I appreciate your help, but your answer doesnt help at all.

“You need to route it through the server” is general advice on CORS issue, I was looking for something more WeWeb specific.

---

<div class="post-metadata">

**Author:** ![jankolros](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/jankolros/32/12612_2.png) [@jankolros](https://community.weweb.io/u/jankolros)\
**Post date:** [February 26, 2025, 8:21am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/8 "2025-02-26T08:21:54Z")

</div>

I am still missing an answer to this point:

> [@jankolros](#):
>
> How does Weweb handle binary file responses in collections? Does Weweb automatically convert binary data to a string? If so, is there a way to prevent this? How can I correctly store and retrieve binary files using Weweb’s collections?

Any ideas?

---

<div class="post-metadata">

**Author:** ![Broberto](https://sea2.discourse-cdn.com/flex016/user_avatar/community.weweb.io/broberto/32/5918_2.png) [@Broberto](https://community.weweb.io/u/Broberto)\
**Post date:** [February 26, 2025, 8:25am UTC](https://community.weweb.io/t/issue-with-downloading-binary-files-from-weweb-collection-cors-binary-string-issue/16674/9 "2025-02-26T08:25:21Z")

</div>

You don’t need my not helpful answers bro. When you hit CORS, it means you need to route it through a server, assuming anything else is dumb. Even when you go through WeWeb’s option to “avoid CORS” what you’re doing is just well guess what … route it through WeWeb’s server.
