0

I have applicaiton which is the combination of MVC 4 + Web Api + SQL server. I am trying to download the doc file to MVC but i have tried the below step. I have Web API where i have written the below code. when i send the rowid it has the value stored in the DB as varbinary. file format can be any thing like .doc,pdf etc ... but however I am looking for the first doc or PDF file format.

When I call the Web api it will create the PDF file and download it , but the file is completely corrupted.

  [ResponseType(typeof(MandateExceptionDO))]       
  [HttpGet]
  [Route("api/DealingMandate/GetExceptionDoc/{rowId}")]
  public HttpResponseMessage GetExceptionDoc(int rowId)
  {
     IDealingMandates repository = new DealingMandatesRepository();
     List<MandateExceptionDO> mandateexceptiondoc =new  List<MandateExceptionDO>();
     mandateexceptiondoc = repository.GetExceptionDoc(rowId);
     HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
     //response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
     //response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");

    //byte[] fileBytes = System.IO.File.ReadAllBytes(mandateexceptiondoc[0].Content);
    response.Content = new ByteArrayContent(mandateexceptiondoc[0].Content);
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    response.Content.Headers.ContentDisposition.FileName = "testing.pdf";
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    //return Ok(mandateexceptiondoc);
    return response;
   }
Guanxi
  • 3,103
  • 21
  • 38
user3227615
  • 207
  • 1
  • 3
  • 11
  • How are you calling this from client side? – Guanxi Dec 01 '15 at 21:50
  • I believe you need to create the instance of httpresponsemessage from Request.CreateResponse(...). See the answer for this question. http://stackoverflow.com/questions/12563576/web-api-content-in-httpresponsemessage – Matthew Brubaker Dec 01 '15 at 22:32
  • i am creating the RestRequest to call the API below is the code. Doc get the null value here var request = new RestRequest("api/DealingMandate/GetExceptionDoc/{rowId}", Method.GET); request.AddParameter("rowId", Id, ParameterType.UrlSegment); var doc = restClient.Execute>(request); – user3227615 Dec 02 '15 at 04:16
  • @MatthewBrubaker i have add the code HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, mandateexceptiondoc); on the MVC side i getting the below error {"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Self referencing loop detected for property 'ApplicationInstance' with type 'ASP.glo – user3227615 Dec 02 '15 at 13:13

1 Answers1

0

I am able to fix this issue on the web api i made the byte as string as show below

  String doc = Convert.ToBase64String(customermandate.Content);

and for the MVC side i converted back to byte from the string

 var doc = restClient.Execute(request);
             var response = doc.Content.Substring(1, doc.Content.Length - 2).Replace(@"\/", "/");
  byte[] docBytes = Convert.FromBase64String(response);
             if (doc.Content != null && doc.Content.Length > 0 && !string.IsNullOrEmpty(doc.Content))
             {
                 Response.Clear();    
                 Response.ContentType = "application/pdf";
                 Response.AddHeader("content-disposition", "attachment; filename=" + FileName);
                 Response.BinaryWrite(docBytes);    
                 Response.End();
          }
user3227615
  • 207
  • 1
  • 3
  • 11