c# - How to return a file from web api controller? -


i'm using mvc 5 web api controller, , want return file:

[route("")] public httpresponsemessage getfile() {     var statuscode = httpstatuscode.ok;     filestream file = xlgeneration.xlgeneration.getxlfileexigence();      return request.createresponse(statuscode, file); } 

it dosn't work.

the exception postman is:

"exceptionmessage": "the 'objectcontent`1' type failed serialize response body content type 'application/json; charset=utf-8'."

try this...

[route("")] public httpresponsemessage getfile() {     var result = new httpresponsemessage(httpstatuscode.ok);     try     {         var file = xlgeneration.xlgeneration.getxlfileexigence();         result.content = new streamcontent(file);         result.content.headers.contenttype = new system.net.http.headers.mediatypeheadervalue("application/octet-stream");         var value = new system.net.http.headers.contentdispositionheadervalue("attachment");         value.filename = "whatever filename is";         result.content.headers.contentdisposition = value;     }     catch (exception ex)     {         // log exception details here         result = new httpresponsemessage(httpstatuscode.internalservererror);     }     return result; } 

this should stream file.


Comments