How to display a server side generated PDF stream in javascript sent via HttpMessageResponse Content

后端 未结 2 1186
别那么骄傲
别那么骄傲 2020-12-14 11:23

On my server side I am using ASP.NET MVC Web Api, where I am generating the PDF file with Crystal report and exporting it to PDF format. The code goes as follows:

         


        
相关标签:
2条回答
  • 2020-12-14 11:35

    This link helped us a lot. Below Solution worked greatly for me. In our case OFF LINE storing in Stream in DB:

        //Reading the exising pdf file   
        byte[] bytes = File.ReadAllBytes(pafTemplate);
        //gettting memory stream object and writing in to it   
        var stream = new MemoryStream();
        stream.Write(bytes, 0, bytes.Length);
        //For our custom need we are placing the memory stream in one of the column
        PdfDataTable.PdfFormDataColum = stream.GetBuffer();
    

    Web-API Code:

    [GET("api/pdf/getPdfRecordData/{pdfId}")] //AttributeRouting
    public HttpResponseMessage GetPdfRecordData(int pdfId) 
    { 
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value"); 
        MemoryStream ms = GetPdfMemoryStreamFromDataBase(pafId); 
        response.Content = new ByteArrayContent(ms.ToArray()); 
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 
        ms.Close(); 
        return response; 
    } 
    

    AngularJs Code:

    $http.get('api/pdf/getPdfRecordData/10', null, { responseType: 'arraybuffer' })
                    .success(function (data) {
                         var file = new Blob([data], { type: 'application/pdf' });
                        var fileURL = URL.createObjectURL(file);
                        window.open(fileURL);
                    });
    
    0 讨论(0)
  • 2020-12-14 12:00

    Seems I did it correctly all the time. The problem was with my angularjs version (v1.08). When upgrading to v1.2 everything worked ok. In v1.08 the responseType: 'arraybuffer' paramater (which is crucial to what I was doing) was simply ignored by angularjs. It seems to be implemented as of v.1.1. See this SO question: How to read binary data in AngularJS in an ArrayBuffer?

    0 讨论(0)
提交回复
热议问题