Next.js Discord

Discord Forum

NextJS wont download a file from base64?

Unanswered
Nile Crocodile posted this in #help-forum
Open in Discord
Nile CrocodileOP
I was converting my app from react to nextjs and one of my functions wont work anymore

  downloadFile("https://spotify-api.mendel.uk/api/v1/audio/change", props, response => {
          console.log(response)

          if (response && response.status === 200) {
              const blob = new Blob([response], {type: 'application/octet-stream'});
              const downloadUrl = URL.createObjectURL(blob);
              const a = document.createElement("a");
              a.href = downloadUrl;
              a.download = "output.mp3";
              document.body.appendChild(a);
              a.click();

          }

      })
  }

    function downloadFile(url: string, body: any, success: (response: any) => void) {
        const xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.responseType = "blob";
        xhr.setRequestHeader('Content-type', 'application/json; charset=utf-8');

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (success) success(xhr.response);
            }
        };

        const blob = new Blob([JSON.stringify(body)], {
            type: "application/json",
        });

        xhr.send(blob);
    }


it used to be able to download files but wont anymore on nextjs, any ideas?

2 Replies

Nile CrocodileOP
also, this error in the server console