2016-01-18 16 views
9

asp.net 5. wep api oluşturuldu.ASP.net 5 web api dosyasından dosyaya nasıl dönülür

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; 

    } 

nasıl yerine JSON yanıt olarak gerçek dosya dönebilirsiniz Ben Test istemcisi olarak Postacı kullanıyorum:? Kod

{ 
    "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 
}` 

` gibi Ama bunun yerine dosyanın tepki görünüyor.

+0

Belki de bu yardımcı olabilir: http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api – jpgrassi

+0

nop, hala olarak json döndürür sonuç: –

cevap

5

yerine HttpResponseMessage ait IActionResult kullandı. Ve FileStreamResult'u döndürdü ve işe koydu.

Yeni bir sorun var, dosya, sunucudan akışla açtığım dosya değil. Ancak bunun için yeni bir soru oluşturacak.

Devam: Return file from ASP.NET 5 Web API

Teşekkür

0

HttpContext.Response.ContentType = "application/pdf" ayarı hiç yardımcı oluyor mu?

Bu ihtiyaçlarıyla uyumlu olmalıdır:

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

hayır, yardımcı olmuyor. Sonuç aynı kalır. –

+0

"Başlıklar": [], 'şu anda dolu bir şey var mı? İlk yazınızdan görebildiğim gibi boş. Asp.net 5'in hangi yapıyı kullanıyorsunuz ve tam olarak. Net veya coreclr mı? –

+0

Hayır, boş kalır. –

1

bu deneyin!

[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; 
    }