In VB.NET, how can I write a memory stream to browser. My memory stream object has data to build a PDF file. Now I want it to be rendered on browser. How to do that?
You could try something like:
Dim stream As MemoryStream = GetMemoryStream()
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition", "attachment; filename=yourfile.pdf")
Response.Write(stream.ToArray())
Response.End()
I have not tested the code nor am I sure of the mime type, but this should get you started.
Related
My REST API returns a PDF document in bytes and I need to call that API and show the PDF document on the ASP page for previewing to the user.
I tried
Response.Write HttpReq.responseBody
but it's writing some unreadable text on the page. The httpReq is my object through which I am calling the REST API.
Response of REST API:
Request.CreateResponse(HttpStatusCode.OK, pdfStream, MediaTypeHeaderValue.Parse("application/pdf"))
You'll have to define the content type of the response as PDF:
Response.ContentType = "application/pdf"
Then write the binary data to the response:
Response.BinaryWrite(httpReq.ResponseBody)
Full example:
url = "http://yourURL"
Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
httpReq.Open "GET", url, False
httpReq.Send
If httpReq.Status = "200" Then
Response.ContentType = "application/pdf"
Response.BinaryWrite(httpReq.ResponseBody)
Else
' Display an error message
Response.Write("Error")
End If
In Classic ASP, Response.Write() is used to send textual data back to the browser using the CodePage and Charset properties defined on the Response object (by default this is inherited from the current Session and by extension the IIS Server Configuration).
To send binary data back to the browser use Response.BinaryWrite().
Here is a quick example (snippet based off you already having the binary from httpReq.ResponseBody);
<%
Response.ContentType = "application/pdf"
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Force the browser to display instead of bringing up the download dialog.
Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
'Write binary from the xhr responses body.
Call Response.BinaryWrite(httpReq.ResponseBody)
%>
Ideally, when using a REST API via an XHR (or any URL for that matter) you should be checking the httpReq.Status to allow you to handle any errors separately to returning the binary, even set a different content-type if there is an error.
You could restructure the above example;
<%
'Make sure nothing in the Response buffer.
Call Response.Clear()
'Check we have a valid status returned from the XHR.
If httpReq.Status = 200 Then
Response.ContentType = "application/pdf"
'Force the browser to display instead of bringing up the download dialog.
Call Response.AddHeader("Content-Disposition", "inline;filename=somepdf.pdf")
'Write binary from the xhr responses body.
Call Response.BinaryWrite(httpReq.ResponseBody)
Else
'Set Content-Type to HTML and return a relevant error message.
Response.ContentType = "text/html"
'...
End If
%>
Content-Disposition:What are the differences between “inline” and “attachment”?
A: ASP Classic, Download big files does not work on certain servers (useful tips on chunking your downloads)
How to convince chrome(browser) to show the download file dialog while downloading a JSON content?
With the following headers chrome renders the JSON directly on the screen. Content-Type:application/json; charset=utf-8
Any ideas?
As it turns out, you only have to set the content disposition on the server end,
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-Disposition: attachment; filename=" + fileName + ".json");
I am trying to achieve this:
I have a website from where users buy files and then they see the download links.
Files are located on another location (some www.myfiles.com) the links are secure so the users dont see where are the files but actually the browser should start downloading the files as soon as they click.
user buy files, click on the link and i do this:
var filename = "SomeHighlySecureFile.mp3";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://myfiles.com/download.aspx?file=" + filename);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
context.Response.Buffer = true;
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + fileinfo.Name);
context.Response.ContentType = "application/octet-stream";
I have no idea, what to do next? coz the "WriteFile" does not provide the options to write another stream?
Can anyone give me a clue how to do that?
Using a streamwriter we are exporting an .csv file.
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-disposition", "attachment; filename=Peuters.csv")
This worked until IE11 was released. The code never changed but in IE I am getting the message at the bottom ".csv couldn't be downloaded". Then when I click on Retry it downloads the complete page, html & js, not the export file.
Anyone found a solution for this?
Yes, already tried these steps: http://answers.microsoft.com/en-us/ie/forum/ie9-windows_7/couldnt-be-downloaded-appears-at-bottom-of-screen/ad51df58-bd7c-e011-9b4b-68b599b31bf5?msgId=61dd62bc-d592-4d54-8bd5-84a22ea59472
Didn't work.
Printscreen:
http://static.dyp.im/ufgMWUAXOb/1b257748b8489c192b53eae37623276c.png
This one is killing me! In ASP Classic I'm trying to stream users a pdf file, so far i've got the following code:
Response.Buffer = False
'This is download
Response.ContentType = ContentTypeFromFile(DownloadFileName)
'Set file name
Response.AddHeader "Content-Disposition", "inline; filename=myfile.pdf#search=fox"
Response.binarywrite "c:/myfile.pdf"
However it's not working and the ? and # are changed to _ when I run it which causes the file download to break.
I found the same question asked on here but it was 3 years ago and I've tried doing what the accepted answer suggested without any success.
Can anyone help me?
Link to the old question: https://stackoverflow.com/search?q=open+pdf+with+search
Okay finally I worked this one out! The answer given in the other question is the actual answer, understanding it however was the tricky part!
Have this code in your page:
<%
response.Clear
Response.Buffer = False
'This is download
Response.ContentType = "application/pdf"
'Set file name
Response.AddHeader "Content-Disposition", "inline; filename=myfile.pdf"
set stream = Server.CreateObject("ADODB.Stream")
stream.Open
stream.Type = 1 ' binary
stream.LoadFromFile("c:\test.pdf")
Response.BinaryWrite(stream.Read)
Response.End()
%>
And then you pass the parameters to the adobe reader through the url! So if the code above is in a page called: default.asp
then do this: http://www.yoururl.com/default.asp#search=fox&zoom=20&page=2
and it will work a treat! Not in google chrome mind you! I googled about getting this sort of thing working in chrome however google didn't code in parameters into their pdf viewer.