file - Return PDF to the Browser using Asp.net core -


i created wep api in asp.net core return pdf. here code:

public httpresponsemessage get(int id) {     var response = new httpresponsemessage(system.net.httpstatuscode.ok);                var stream = new system.io.filestream(@"c:\users\shoba_eswar\documents\request.pdf", system.io.filemode.open);     response.content = new streamcontent(stream);     response.content.headers.contentdisposition = new system.net.http.headers.contentdispositionheadervalue("attachment");     response.content.headers.contentdisposition.filename = "newtab";     response.content.headers.contenttype = new system.net.http.headers.mediatypeheadervalue("application/pdf");     return response; } 

but returns json response:

{    "version":{       "major":1,       "minor":1,       "build":-1,       "revision":-1,       "majorrevision":-1,       "minorrevision":-1    },    "content":{       "headers":[          {             "key":"content-disposition",             "value":[                "attachment; filename=newtab"             ]          },          {             "key":"content-type",             "value":[                "application/pdf"             ]          }       ]    },    "statuscode":200,    "reasonphrase":"ok",    "headers":[     ],    "requestmessage":null,    "issuccessstatuscode":true } 

am doing wrong here?

as explained in asp.net core httprequestmessage returns strange json message, asp.net core not support returning httpresponsemessage (what package did install access type?).

because of this, serializer writing public properties of httpresponsemessage output, other unsupported response type.

to support custom responses, must return iactionresult-implementing type. there's plenty of those. in case, i'd filestreamresult:

public iactionresult get(int id) {     using (var stream = new filestream(@"path\to\file", filemode.open))     {         return new filestreamresult(stream, "application/pdf");     } } 

or use physicalfileresult, stream handled you:

public iactionresult get(int id) {     return new filestreamresult(@"path\to\file", "application/pdf"); } 

of course of can simplified using helper methods, such controller.file():

public iactionresult get(int id) {     using (var stream = new filestream(@"path\to\file", filemode.open))     {         return file(stream, "application/pdf", "filedownloadname.ext");     }        } 

this abstracts creation of filecontentresult or filestreamresult (for overload, latter).

or if you're converting older mvc or web api application , don't want convert code @ once, add reference webapicompatshim (nuget) , wrap current code in responsemessageresult:

public iactionresult get(int id) {     var response = new httpresponsemessage(httpstatuscode.ok);                var stream = ...     response.content...      return new responsemessageresult(response); } 

if don't want use return file(filename, contenttype, filedownloadname), filestreamresult doesn't support setting content-disposition header constructor or through properties.

in case you'll have add response header response before returning file result:

var contentdisposition = new contentdispositionheadervalue("attachment"); contentdisposition.sethttpfilename("foo.txt"); response.headers[headernames.contentdisposition] = contentdisposition.tostring(); 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -