# Downloading blocked by CSP

**URL:** <https://community.dynatrace.com/t/downloading-blocked-by-csp/48808>\
**Category:** Developer Q&A Forum\
**Tags:** custom-app\
**Created:** [November 8, 2024, 2:05pm UTC](https://community.dynatrace.com/t/downloading-blocked-by-csp/48808 "2024-11-08T14:05:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Auriane](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Auriane](https://community.dynatrace.com/u/Auriane)\
**Post date:** [November 8, 2024, 2:05pm UTC](https://community.dynatrace.com/t/downloading-blocked-by-csp/48808/1 "2024-11-08T14:05:28Z")

</div>

Hello,

In a custom App, I retrieve some JSON content from an API, and I want the user to be able to download a JSON file of this data.

I tried a few things like this :

```javascript
const downloadTxtFile = () => {
  const texts = ["line 1", "line 2", "line 3"]// file object
  const file = new Blob(texts, { type: 'text/plain' });// anchor link
  const element = document.createElement("a");
  element.href = URL.createObjectURL(file);
  element.download = "100ideas-" + Date.now() + ".txt";// simulate link click
  document.body.appendChild(element); // Required for this to work in FireFox
  element.click();
}

```

But each time I got a CSP error :

```markup
Content-Security-Policy : Les paramètres de la page ont empêché le chargement d’une ressource (frame-src) à l’adresse blob:http://localhost:3000/c967a477-517c-4a45-b661-0db72d23a37a car elle enfreint la directive suivante : « frame-src 'self' https://{environmentid}.live.dynatrace.com https://sso.dynatrace.com https://chat-one.dynatrace.com https://dev.access-approval.internal.dynatracelabs.com https://hardening.access-approval.internal.dynatracelabs.com https://access-approval.internal.dynatrace.com http://localhost:3000 »

```

I tried to change the CSP rules, but it didn’t work and according to [this](https://developer.dynatrace.com/develop/security/configure-csp-rules/) it might not be possible to change frame-src.

Is there an alternative approach to allow file downloads without running into CSP restrictions?

---

<div class="post-metadata">

**Author:** ![haris](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/haris/32/38293_2.png) [@haris](https://community.dynatrace.com/u/haris)\
**Post date:** [November 11, 2024, 12:42pm UTC](https://community.dynatrace.com/t/downloading-blocked-by-csp/48808/2 "2024-11-11T12:42:01Z")

</div>

Hi Auriane,

I’ll update you soon with a better answer. For now, can you try to do&nbsp;a workaround which is to create an iframe with a download link instead of having it in the main document:

Some explanation: [CSP frame-src blocked on Firefox for blob on Embedded app · Issue #11 · Shopify/shopify-app-bridge · GitHub](https://github.com/Shopify/shopify-app-bridge/issues/11#issuecomment-710605337)

```javascript
function downloadBlobFile(file) {
const blobUrl = URL.createObjectURL(file);

const iframe = document.createElement('iframe');

iframe.style.display = 'none';

iframe.addEventListener('load', () => {

const a = iframe.contentDocument.createElement('a');

a.href = blobUrl;
a.download = file.name;

iframe.contentDocument.body.appendChild(a);

a.click();
})

document.body.appendChild(iframe);

setTimeout(() => {
document.body.removeChild(iframe);
}, 1000);
}

const file = new File(['Hello, world!'], "aaa.json",{type: 'application/json'});
downloadBlobFile(file);

```

---

<div class="post-metadata">

**Author:** ![Auriane](https://avatars.discourse-cdn.com/v4/letter/a/e9bcb4/32.png) [@Auriane](https://community.dynatrace.com/u/Auriane)\
**Post date:** [November 12, 2024, 9:05am UTC](https://community.dynatrace.com/t/downloading-blocked-by-csp/48808/3 "2024-11-12T09:05:38Z")

</div>

It works !

Thank you
