question

GirishKhadse-1617 avatar image
0 Votes"
GirishKhadse-1617 asked BruceZhang-MSFT answered

Web API 2 : Returning binary file with “application/octet-stream” not working with IIS Server : IIS/10.0 but working with IIS/8.5

I have web api which returns binary file, which is consumed by Angular 12 client. Application is deployed on Dev and Integ environments. Now suprisingly, same api is working on Dev but not on Integ. When on Integ, instead of returning binary file, it returns JSON. The only difference is Dev IIS Server (hosting API) version is 8.5 while that of Integ is 10.0.

I have also added necessary response header like :

  result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

Below is the sample response returned on Integ :

 {
   "Version": {
     "_Major": 1,
     "_Minor": 1,
     "_Build": -1,
     "_Revision": -1
   },
   "Content": {
     "Headers": [
       {
         "Key": "Content-Type",
         "Value": [
           "application/octet-stream"
         ]
       }
     ]
   },
   "StatusCode": 200,
   "ReasonPhrase": "OK",
   "Headers": [],
   "RequestMessage": null,
   "IsSuccessStatusCode": true
 }


Below is my API code :

  [HttpPost]
         [Route("api/Chart/test")]
         public async Task<HttpResponseMessage> test(TestEntity request)
         {
             try
             {
                 var ms = await _service.testMethod(request.Data, request.ExportType, request.FileName);
                 var result = new HttpResponseMessage(HttpStatusCode.OK)
                 {
                     Content = new ByteArrayContent(ms.ToArray())
                 };
                 result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                 return result;
             }
             catch (Exception)
             {
                 var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                 return result;
             }
         }

And below is my angular method code :

  angularTestMethod(request) {
     return this.http.post(`${this.apiUrl}api/Chart/test`, request, { withCredentials: true, responseType: 'blob'})
     .pipe(catchError(err => this.handleError(err)));
   }


Any idea what could be the issue? As code of angular and web api is same for both environments.

Below are the version details:

Angular : Angular 12,
Web API : ASP.Net Web API 2.2
.Net Framework : 4.6.1
Dev Windows Server and IIS : 2012 and 8.5 resp
Integ Windows Server and IIS : 2016 and 10.0 resp


As asked in the comment, Below is another example of issue I am getting :

The TestEntity Class is a complex request object. Rather I will provide another method giving me same issue. Its returning JSON instead of Binary File.

  [HttpGet, Route("api/Chart/download/{id}")]
         public HttpResponseMessage Download(int id)
         {
             try
             {
                 var ms = _chartService.Download(id);
                 var result = new HttpResponseMessage(HttpStatusCode.OK)
                 {
                     Content = new ByteArrayContent(ms.ToArray())
                 };
                 result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                 return result;
             }
             catch (FileNotFoundException)
             {
                 var result = new HttpResponseMessage(HttpStatusCode.NotFound);
                 return result;
             }
             catch (Exception)
             {
                 var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                 return result;
             }
         }

And below is Download method :



    public MemoryStream Download(int id)
             {
                 var chart= _repository.GetById(id);
                 if (chart != null)
                 {
                     var stream = new MemoryStream();
                     var writer = new StreamWriter(stream);
                     writer.Write(chart.MetaData);
                     writer.Flush();
                     stream.Position = 0;
                     return stream;
                 }
        
                 throw new FileNotFoundException("Chart not found");
             }

Chart Class :

  public class Chart
     {
         public int Id { get; set; }
         public string Name { get; set; }
         public string MetaData { get; set; }
         public int? ChartFolderId { get; set; }
         public int? SortOrder { get; set; }
         public bool IsPublic { get; set; }
         public bool InActive { get; set; }
         public string Owner { get; set; }
         public string InActiveBy { get; set; }
         public DateTime? InActiveOn { get; set; }
         public string InsertedBy { get; set; }
         public DateTime InsertedOn { get; set; }
         public string ModifiedBy { get; set; }
         public DateTime ModifiedOn { get; set; }
     }






dotnet-aspnet-mvcwindows-server-iis-configurationdotnet-aspnet-webapi
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @GirishKhadse-1617 ,

Can you show the property of TestEntity class? I also need method testMethod in _service so that I can reproduce the issue in my IIS10.


0 Votes 0 ·

Earlier method was too complex to provide necessary information. I have provided you with another example where I am getting same issue. Download method gets memory stream and should return it as Binary File but not working..

0 Votes 0 ·

I have updated the existing question with another sample method which is giving same issue.

0 Votes 0 ·

1 Answer

BruceZhang-MSFT avatar image
0 Votes"
BruceZhang-MSFT answered

Hi @GirishKhadse-1617 ,

I test the code in my local visial studio. It return like this.
126579-2021-08-25-175559.jpg

Then I deploy it on IIS8.5(windows server2012) and IIS10(windows server2016).
126681-1.jpg
126649-3.jpg

Both of them have same result. I think you can try another server to test again. You can use IIS10 on windows server2019.



1.jpg (32.8 KiB)
3.jpg (64.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.