2016-01-18 10 views
9

से फ़ाइल कैसे वापस करें एएसपीनेट 5 में वीपी एपीआई बनाया गया है। मैं पोस्ट अनुरोध के लिए फ़ाइल प्रतिक्रिया वापस करने की कोशिश कर रहा हूं। लेकिन फ़ाइल के बजाय प्रतिक्रिया लग रहा है तरह `एएसपीनेट 5 वेब एपीआई

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

कोड:

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; 

    } 

मैं कैसे JSON के बजाय प्रतिक्रिया के रूप में वास्तविक फ़ाइल लौट सकते हैं मैं परीक्षण ग्राहक के रूप में डाकिया उपयोग कर रहा हूँ?।

+0

शायद यह मदद कर सकते हैं: http://stackoverflow.com/questions/9541351/returning-binary-file-from-controller-in-asp-net-web-api – jpgrassi

+0

nop, यह अभी भी json के रूप में देता है परिणाम: –

उत्तर

5

IActionResult इस्तेमाल किया बजाय HttpResponseMessage की। और FileStreamResult लौटा, और यह काम कर रहा है।

एक नई समस्या है, फ़ाइल मैं सर्वर से स्ट्रीम के साथ खोलने वाला नहीं है। लेकिन इसके लिए एक नया सवाल तैयार करेगा।

जारी है: Return file from ASP.NET 5 Web API

धन्यवाद

0

HttpContext.Response.ContentType = "application/pdf" सेटिंग में मदद करता है?

यह आपकी आवश्यकताओं को करना चाहिए:

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

नहीं, यह मदद नहीं करता है। परिणाम वही रहता है। –

+0

क्या "हेडर" है: [], अब कुछ भी आबादी है? जैसा कि मैं आपकी प्रारंभिक पोस्ट से देख सकता हूं कि यह खाली है। आप एएसपीनेट 5 का क्या उपयोग कर रहे हैं, और क्या यह पूर्ण .net या coreclr है? –

+0

नहीं, यह खाली रहता है। –

1

इस कोशिश करो!

[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; 
    } 
संबंधित मुद्दे