2016-01-18 9 views
9

Erstellt wep api in asp.net 5. Ich bin tring, Dateiantwort für Post-Anfrage zurückzugeben. Aber statt Datei sieht die Antwort wie `Wie man Datei von ASP.net zurückgibt 5 web api

{ 
    "version": { 
    "major": 1, 
    "minor": 1, 
    "build": -1, 
    "revision": -1, 
    "majorRevision": -1, 
    "minorRevision": -1 
    }, 
    "content": { 
    "headers": [ 
     { 
     "key": "Content-Disposition", 
     "value": [ 
      "attachment; filename=test.pdf" 
     ] 
     }, 
     { 
     "key": "Content-Type", 
     "value": [ 
      "application/pdf" 
     ] 
     } 
    ] 
    }, 
    "statusCode": 200, 
    "reasonPhrase": "OK", 
    "headers": [], 
    "requestMessage": null, 
    "isSuccessStatusCode": true 
}` 

Code:

public HttpResponseMessage Post([FromBody]DocumentViewModel vm) 
    { 
     try 
     { 
      if (ModelState.IsValid) 
      { 

       var Document = _repository.GetDocumentByGuid(vm.DocumentGuid, User.Identity.Name); 
       var Params = Helper.ClientInputToRealValues(vm.Parameters, Document.DataFields); 
       var file = Helper.GeneratePdf(Helper.InsertValues(Params, Document.Content));     

       var result = new HttpResponseMessage(HttpStatusCode.OK) 
       { 
        Content = new ByteArrayContent(System.IO.File.ReadAllBytes(file)) 
       }; 
       result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") 
       { 
        FileName = "test.pdf" 
       }; 
       result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf"); 
       return result; 

      } 

     } 
     catch (Exception ex) 
     { 
      Response.StatusCode = (int)HttpStatusCode.BadRequest; 
      return null; 
     } 
     Response.StatusCode = (int)HttpStatusCode.BadRequest; 
     return null; 

    } 

Wie kann ich die reale Datei als Reaktion statt JSON kehre ich bin mit Postman als Test-Client?.

+0

Vielleicht kann dies helfen: http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api – jpgrassi

+0

nop, es gibt noch json als Ergebnis: –

Antwort

5

verwendet IActionResult statt HttpResponseMessage. Und FileStreamResult zurückgegeben, und es funktioniert.

Ich habe ein neues Problem, die Datei ist nicht die, die ich mit dem Stream vom Server öffnen. Aber dafür wird eine neue Frage entstehen.

geht weiter: Return file from ASP.NET 5 Web API

Dank

0

Hilft die Einstellung HttpContext.Response.ContentType = "application/pdf" überhaupt?

Dies sollte Ihre Bedürfnisse anzupassen:

public FileResult TestDownload() 
    { 
     HttpContext.Response.ContentType = "application/pdf"; 
     FileContentResult result = new FileContentResult(System.IO.File.ReadAllBytes("YOUR PATH TO PDF"), "application/pdf") 
     { 
      FileDownloadName = "test.pdf" 
     }; 

     return result;         
    } 
+0

Nein, es hilft nicht. Ergebnis bleibt gleich. –

+0

Hat '" headers ": [],' jetzt etwas gefüllt? Wie ich aus Ihrem ersten Post sehen kann, ist es leer. Was Build von asp.net 5 verwenden Sie, und ist es voll. NET oder corclr? –

+0

Nein, es bleibt leer. –

1

Try this!

[HttpGet] 
    public HttpResponseMessage Get() 
    { 
     var fs = new FileStream(myfileInfo.FullName, FileMode.Open, FileAccess.Read, 
           FileShare.Read, 32768, true); 
      var response = this.Request.CreateResponse(); 
      response.Content = new StreamContent(fs); 
      response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment"); 
      response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); 
      response.Content.Headers.ContentLength = fs.Length; 
      fs.Dispose(); 
      return response; 
    }