asp.net mvc - HttpResponseMessage Content won't display PDF -
i have created web api returns httpresponsemessage in content set pdf file. if call web api directly works great , pdf rendered in browser.
response.content = new streamcontent(new filestream(pdflocation, filemode.open, fileaccess.read)); response.content.headers.contenttype = new mediatypeheadervalue("application/pdf"); response.headers.connectionclose = true; return response;
i have mvc client contact web api, request pdf file render user in same way above.
unfortunately, i'm not sure problem though set the content-type:
response.content.headers.contenttype = new mediatypeheadervalue("application/pdf");
when click on link calls web api, text rendering of httpresponsemessage.
statuscode: 200, reasonphrase: 'ok', version: 1.1, content: system.net.http.streamcontent, headers: { connection: close content-disposition: attachment content-type: application/pdf }
i'm thinking client application missing setting allow render pdf web api does...
any appreciated. thanks
after hours of google searching trial , error, have resolved issue here.
instead of setting content of response streamcontent have changed bytearraycontent on web api side.
byte[] filebytes = system.io.file.readallbytes(pdflocation); response.content = new bytearraycontent(filebytes); response.content.headers.contentdisposition = new contentdispositionheadervalue("attachment"); response.content.headers.contentdisposition.filename = filename; response.content.headers.contenttype = new mediatypeheadervalue("application/pdf");
by doing way, mvc 4 application able download pdf using webclient , downloaddata method.
internal byte[] downloadfile(string requesturl) { string serverurl = _baseaddress + requesturl; var client = new system.net.webclient(); client.headers.add("content-type", "application/pdf"); return client.downloaddata(serverurl); }
the byte[] array returned can converted memorystream before returning file output...
response.addheader("content-disposition", "inline; filename="+filename); memorystream outputstream = new memorystream(); outputstream.write(file, 0, file.length); outputstream.position = 0; return file(outputstream, "application/pdf");
i hope can useful others have wasted lot of time working.
Comments
Post a Comment