Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a pdf from WebAPI and read the pdf from the MVC Controller?

I have a Web API service that should return a PDF.
I am then trying to call that WebAPI method to read the PDF.

Here is my API Method:

    [HttpPost]
    [Route("GetTestPDF")]
    public HttpResponseMessage TestPDF()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(@"C:\MyPath\MyFile.pdf", FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = "MyFile.pdf";
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");

        return Request.CreateResponse(HttpStatusCode.OK, response);
    }

However when I go to read the response I don't see the pdf contents. I am not sure where I am going wrong with this.

Controller Method:

public ActionResult GetPDF()
        {
            var response = new HttpResponseMessage();

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(@"my local host");                               
                response = httpClient.PostAsync(@"api/job/GetTestPDF", new StringContent(string.Empty)).Result;
            }

            var whatisThis = response.Content.ReadAsStringAsync().Result;
            return new FileContentResult(Convert.FromBase64String(response.Content.ReadAsStringAsync().Result), "application/pdf");
        }

When I examine whatisThis variable I see the content type and the content dispostion correctly set from my API. However I don't see the content of the PDF.

How can I read the PDF content?

EDIT:

If I read the content as a string in my MVC site I see. (I don't see the actual content of the file)

{"Version":{"_Major":1,"_Minor":1,"_Build":-1,"_Revision":-1},"Content":{"Headers":[{"Key":"Content-Disposition","Value":["attachment; filename=MyFile.pdf"]},{"Key":"Content-Type","Value":["application/pdf"]}]},"StatusCode":200,"ReasonPhrase":"OK","Headers":[],"RequestMessage":null,"IsSuccessStatusCode":true}

I stepped through the WebAPI and it is successfully reading and setting the response.Content with the file contents.

Still not sure if this is an issue on the WebAPI side or the MVC side.

like image 504
John Doe Avatar asked Feb 04 '23 17:02

John Doe


1 Answers

I'll post this initially as an answer because it's easier to format code!
I made an API endpoint to return a PDF file, and if I call it from a browser the file opens as expected.
As your API doesn't appear to do this, let's assume that the problem is there, and hence this.

Here is the endpoint code, that is very similar to yours, but missing the ContentDisposition stuff:

public HttpResponseMessage Get()
{
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    FileStream fileStream = File.OpenRead("FileName.pdf");
    response.Content = new StreamContent(fileStream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return response;
}
like image 149
Craig H Avatar answered Feb 07 '23 08:02

Craig H