1

Web api has this method;

public HttpResponseMessage GetReport()
{
     var data = Logic.GetReportAsByteArray(...);

     HttpResponseMessage response = new HttpResponseMessage();
     response.Content = new ByteArrayContent(data);
     response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
     response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
     response.Content.Headers.ContentDisposition.FileName = "filename.pdf";
     response.StatusCode = HttpStatusCode.OK;

     return response;
}  

Web client consumes through this method;

...

var request = new HttpRequestMessage(...)

HttpClient client = new HttpClient();
var response = await client.SendAsync(request);

var data = await response.Content.ReadAsByteArrayAsync();
return File(data, "application/pdf", "filename.pdf");

...  

When I run on local I get pdf file as output via my web client. And via Swagger I can also get file output. But when I deploy on IIS server then I get blank pdf via my web client and via Swagger I get no file at all and response body has only the next one:

 {
  "Version": {
    "_Major": 1,
    "_Minor": 1,
    "_Build": -1,
    "_Revision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Type",
        "Value": [
          "application/pdf"
        ]
      },
      {
        "Key": "Content-Disposition",
        "Value": [
          "attachment; filename=filename.pdf"
        ]
      }
    ]
  },
  "StatusCode": 200,
  "ReasonPhrase": "OK",
  "Headers": [],
  "RequestMessage": null,
  "IsSuccessStatusCode": true
} 
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Y.Y.
  • 664
  • 1
  • 6
  • 22
  • try to write a trace on your server when you read the byte array, seems that the pdf file not exist or the array answered by Logic.GetReportAsByteArray(...); is emplty. – Legion Jun 28 '19 at 14:59
  • @Legion No. I checked there. I get the correct data – Y.Y. Jun 28 '19 at 15:01
  • 1
    You might need to add a mime type entry in your web.config - https://blogs.iis.net/bills/how-to-add-mime-types-with-iis7-web-config (for you '.pdf' and 'application/pdf' ) – JsAndDotNet Jun 28 '19 at 15:09
  • What version of asp.net are you using. If core then you need to change the return type being used. – Nkosi Jun 28 '19 at 16:54
  • @HockeyJ These settings are already on IIS server – Y.Y. Jun 28 '19 at 22:18
  • @Nkosi Web client is .Net core and Web api is .Net Framework (4.7.1) – Y.Y. Jun 28 '19 at 22:19
  • @Y.Y. are you using `ApiController `or `Controller` – Nkosi Jun 28 '19 at 23:54
  • @Nkosi ApiController – Y.Y. Jun 29 '19 at 08:02
  • If I have deployed on IIS 7.5 then it will work. But if I have deployed on IIS version 10.0 then I get a different response. It does not work. I have not yet been able to find the reason. – Y.Y. Jul 01 '19 at 19:15
  • @Y.Y. I'm having now exactly the same problem. Did you find a solution? – Xavier Egea Apr 01 '20 at 21:42

0 Answers0