0

I am sending a post request to the backend through axios to fetch a Multipart/form-data type response where I receive 1. An Application/json type data and 2. An Application/octet-stream type data which basically is a pdf. Following is the response type passed to the axios post request

responseType: "multipart/form-data",

The response is in the form of string as follows:

--8VsLrsii3Po8wSx8HnipQLwYFlOFDP_FJ-xrfQUv
Content-Disposition: form-data; name="dati"
Content-Type: application/json

[{}]
--8VsLrsii3Po8wSx8HnipQLwYFlOFDP_FJ-xrfQUv
Content-Disposition: form-data; name="pdf"
Content-Type: application/octet-stream
Content-Length: 8481

%PDF-1.5
%����
4 0 obj
<</Filter/FlateDecode/Length 3834>>stream
x���Mo�����wS Xz��\�m8@�V�)�l5PK��h~}I��Α��5��&lү�gμ���4{����K������7�����O���퇛�no~�q�o�?]�3���ۏ7���k.��������\�m�l���M�8͗��q�׋�z��N�����n��}����������w�����7_��|�����}�J���/��������&���h&L��Wּrƹ�JɎ�^��G.................................
%%EOF

--8VsLrsii3Po8wSx8HnipQLwYFlOFDP_FJ-xrfQUv--

I am able to seperate the json part and the pdf part and i am able to get the name, content-type and the actual json data as well as the pdf part. I am using the following code to access the pdf

var data = response // response is the data from the endpoint
var binaryLen = data.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
     var ascii = data.charCodeAt(i);
     bytes[i] = ascii;
}

var pdfDownload = new Blob([bytes], { type: "application/pdf" });
var url = window.URL || window.webkitURL;
var link = url.createObjectURL(pdfDownload);
var a = document.createElement("a");
a.setAttribute("href", link);
a.setAttribute("target", "_blank");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

And I am getting an empty pdf

empty pdf image

Does anyone know a way for me to be able to get correct pdf?

kissu
  • 40,416
  • 14
  • 65
  • 133
  • Does [this](https://stackoverflow.com/a/68832895/7006384) solve your problem. For pdf and images `responseType blob` is very important or you will see empty pages in browsers. – Ankit.Z Oct 18 '22 at 06:13
  • hi. yes so the response was corrupted and thus i was unable to get any data. we then changed the response to receive the pdf as base64 text and use that to get the pdf. – a.kakakhel Oct 24 '22 at 06:44

0 Answers0