1

Hi I have issues opening pdf from byte array sent by WebAPI.

My Service:

getPdfDocument(): Observable<any> {
        return this.httpClient
            .get(this.configuration.serverUrl + this.configuration.getPdfDoc, {
                responseType: "arraybuffer" //tried with 'blob'
            });
}

My component:

this.service.getPdfDocument()
        .subscribe(data => {
            var file = new Blob([data], { type: 'application/pdf' });   
            this.pdfContent = URL.createObjectURL(file);
            window.open(this.pdfContent);
        })

When I run it I get failed to load PDF document... I enabled pop ups still no joy...

enter image description here

1 Answer 1

4

Try this one:

service:

getPdfDocument(): Observable<any> {
    let headers = new HttpHeaders({ 'Content-Type': 'application/JSON' });
    return this.httpClient
               .get(this.configuration.serverUrl + this.configuration.getPdfDoc,
                    { headers: headers, responseType: 'blob' as 'json', observe: 'response' as 'body' }
                });
        }

request:

this.service.getPdfDocument()
        .subscribe(
                (data) => {
                    this.openFileForPrint(data);
                });

openFileForPrint(data: HttpResponse<any>) {
        let fileUrl = window.URL.createObjectURL(data);
        window.open(fileUrl, '_blank', 'location=yes,height=600,width=800,scrollbars=yes,status=yes');
    }

Server side

[HttpGet]
public HttpResponseMessage getpdf(DateTime datum, int idlokacija)
{
    var r = _printService.getdata(datum, idlokacija);
    if (r == null)
    {
        return new HttpResponseMessage(HttpStatusCode.NotFound);
    }
    return SendPdfFile(r);
}

public static HttpResponseMessage SendPdfFile(string filePath, bool brisanje = true)
{
    var stream = new FileStream(filePath, FileMode.Open);
    HttpResponseMessage response = new FileHttpResponseMessage(filePath, brisanje)
    {
        StatusCode = HttpStatusCode.OK,
        Content = new StreamContent(stream)
    };
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    return response;
}
Sign up to request clarification or add additional context in comments.

7 Comments

This implementation throws the following: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
@tom33pr when you look to your server response how does your response header look like?
Joe see below: Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: Content-Type Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS Access-Control-Allow-Origin: * Cache-Control: no-cache Content-Length: 230962 Content-Type: application/json; charset=utf-8 Date: Fri, 19 Oct 2018 11:05:35 GMT Expires: -1 Pragma: no-cache Server: Microsoft-IIS/10.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcZGFlcmEtcHJ6ZXpkemllY2tpdFxTb3VyY2VcUmVwb3NcUmV2aWV3IG9mIERlY2lzaW9uczJcUm9EXFJvRFdTXGFwaVxHZXRUcmltRG9jdW1lbnQ=?=
My WebApi controller returns byte array: [HttpGet] [Route("api/GetDocument")] public byte[] Get()
@tom33pr if you can edit api side of code in my edited answer you will find how my api side looks like.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.