2

I've the following problem. I perform an Ajax call from JS to a aspx page, passing as parameter a base64 encoded pdf. My calling function is the following:

function PDF_Generator(base64)
{

var ESBParams = 
{
    encodedString : base64
}

try
{
    $.ajax({
        method      : "POST",
        url         : pws_PDF_parsing_url,
        data        : ESBParams,
        dataType    : "html",
        async       : "false",
        error       :
        function (xhr, err, errth){
            alert('ERROR:' + errth);
        },
        success     :
        function (success){
            window.open(success);
        }
    });
}
catch(obj)
{
    alert(obj.messsage);
}
}

Now, I have the following aspx page which decode the base64 string, and I would come back the stream of decoded pdf to javascript function, in order to visualise it in a new page (or in an another way - I'll appreciate any advices).

using System;

public class PDF_Generator : System.Web.UI.Page
{
private string decodedPDF;
private string base64EncodedPDF;
Decoder decoder = new Decoder();

protected void Page_Load(object sender, EventsArgs e)
{
    try
    {
        this.base64EncodedPDF = Request.Params["encodedString"]; //get encoded string from js
        this.decodedPDF = decoder.decodeFromBase64toString(this.base64EncodedPDF); //decode string

        byte[] pdfByteStream = decoder.getBytesFromString(this.decodedPDF);

        Page.Response.ClearContent();
        Page.Response.ContentType = "application/pdf";
        Page.Response.AddHeader("Content-Disposition", "inline; filename=" + "summary.pdf");
        Page.Response.AddHeader("Content-Length", pdfByteStream.Length.toString());
        Page.Response.BinaryWrite((byte[])pdfByteStream);
        Page.Response.End();
    }
}
}
giograno
  • 1,749
  • 3
  • 18
  • 30
  • As the PDF is already on the client, is there any reason why you post the document to the server other than opening it in the browser? If the PDF is large, you are transferring a lot of data to and from the server. If there is no other reason, maybe there is a way to just open the PDF on the client without a roundtrip to the server. Maybe this question and the answer helps: http://stackoverflow.com/q/21628378/642579 – Markus Nov 19 '15 at 08:22
  • I post the base64 string to the server with the aim to decode it. Is it possible to display the pdf with JS (or Angular, as your question suggests) without decode the pdf from base64? – giograno Nov 19 '15 at 08:40
  • How do you get the Base64-string? You should be able to convert it to a Blob (might be browser dependent) and use it similar as described in the link in my last comment. On how to convert the Base64 to a Blob see this link http://stackoverflow.com/q/16245767/642579. – Markus Nov 19 '15 at 10:57
  • I tried to convert my string into a Blob, but I discovered that my target browser (IE 8.0) does not support Blobs. Any advices or possible alternatives? – giograno Nov 19 '15 at 15:54

0 Answers0