1

I am working with an ASP .NET Web API where the POST function of the API is returning a byte array like so in the C# code:

return Ok(outputStream.ToArray());

Where outputStream is of type System.IO.MemoryStream and the ToArray() is returning a byte array.

This function is returning a PDF stream that I want to display to the users in their browser or at least let them save it.

I've ran across a JavaScript example from Stack Overflow that shows how to get a Blob from a Base64 encoded string and display it and I was able to get it working when the API returned a Base64 encoded string; however, I wasn't able to get it working when the API was return the byte array as shown before.

May I get some code examples of how to get it working when the API is returning a byte array?

Community
  • 1
  • 1
methon.dagger
  • 505
  • 9
  • 28

1 Answers1

0

My JQuery Ajax POST request looks like:

function LoadPDF() {
        var args = [];
        var x = $.ajax({
            type: "POST",
            url: "DirectoryQuery.aspx/GetFullPdf",
            contentType: "application/json; charset=UTF-8",
            responseType: "text/plain; charset=UTF-8",
            async: true,
            dataType: "json",                             
            processData: "false",
            success: OnSuccess,
            error: OnErrorCall
        }); 
        function OnSuccess(response) {
            var byteArray = new Uint8Array(response.d);

            saveTextAsFile("document.pdf", byteArray);
        }
        function OnErrorCall(response) {
            console.log(response);                
            //location.reload();
        }
    }

Try file-save-as.js, like this:

function saveTextAsFile(fileNameToSaveAs, textToWrite) {
    /* Saves a text string as a blob file*/
    var ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
      ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
      ieEDGE = navigator.userAgent.match(/Edge/g),
      ieVer = (ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));

    if (ie && ieVer < 10) {
        console.log("No blobs on IE ver<10");
        return;
    }

    var textFileAsBlob = new Blob([textToWrite], {
        type: 'text/plain'
    });

    if (ieVer > -1) {
        window.navigator.msSaveBlob(textFileAsBlob, fileNameToSaveAs);

    } else {
        var downloadLink = document.createElement("a");
        downloadLink.download = fileNameToSaveAs;
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = function (e) { document.body.removeChild(e.target); };
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
        downloadLink.click();
    }
}
user1730026
  • 389
  • 3
  • 3
  • Sorry, it doesn't seem to work. I am also getting this error message when I tried to set the responseType to what you specified in your example: `The provided value 'text/plain; charset=UTF-8' is not a valid enum value of type XMLHttpRequestResponseType.` – methon.dagger May 15 '16 at 23:20
  • Honestly, I don't know why text/plain; is an invalid value for you; maybe try removing the "charset=UTF-8" and just using 'text/plain', with the hope that UFT-8 is the default? – user1730026 May 24 '16 at 22:00
  • Modified my answer to include a file-save-as.js implementation. Hope it helps – user1730026 May 24 '16 at 22:06