Couldn't be downloaded IE - vb.net

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

Related

How to show a byte stream response

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?

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");

Stream PDF to browser and utilise Adobe PDF parameters such as #search, #zoom, #page

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.

Rendering memory stream to browser in VB.NET

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.

pdf report view inside browser

I have created this report using BIRT and phpjavabridge
<?php
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=downloaded.pdf");
require_once("http://127.0.0.1:8080/JavaBridge/java/Java.inc");
header("Content-type: text/html");
// the report file to render
$myReport = "test.rptdesign";
// load resources, .rpt files and images from the current working dir
$here = getcwd();
$ctx = java_context()->getServletContext();
$birtReportEngine = java("org.eclipse.birt.php.birtengine.BirtEngine")->getBirtEngine($ctx);
java_context()->onShutdown(java("org.eclipse.birt.php.birtengine.BirtEngine")->getShutdownHook());
// Create a HTML render context
try{
// Load the report design
$design = $birtReportEngine->openReportDesign("${here}/${myReport}");
$task = $birtReportEngine->createRunAndRenderTask( $design );
$task->setParameterValue("sample", new java("java.lang.String", "Hello world!"));
// Add HTML render options
$options = new java("org.eclipse.birt.report.engine.api.PDFRenderOption");
$options->setOutputFormat($options->OUTPUT_FORMAT_PDF);
// Create the output
$out = new java("java.io.ByteArrayOutputStream");
$options->setOutputStream($out);
$task->setRenderOption($options);
$task->run ();
$task->close();
} catch (JavaException $e) {
echo $e; //"Error Calling BIRT";
}
// Return the generated output to the client
echo java_values($out->toByteArray());
?>
The report viewed perfectly inside Internet Explorer 8.0 as it triggered the Adobe Acrobat plugin. The problem is when I opened the report inside Mozilla Firefox 3.5.4 and Google Chrome 4.0.233 it showed me the binary string content of the pdf file, instead of triggering the Adobe Acrobat plugin.
I have checked this by putting a pdf file in the htdoc folder and call it from Firefox and Chrome, it worked just fine. But why the header wont work for the report?
*Also why the header only work for IE 8.0? I need the report to be viewed in all major browser
Content-type should be "application/pdf"
(or "application/x-pdf", for more recent pdf formats)
I think that if you change
header("Content-type: text/html");
to
header("Content-type: application/pdf");
in the code above, the browsers mentioned should start rendering the pdf documents properly (provided they are so configured), and IE will continue working ok (IE has some Automatic MIME Type Detection (based on the first few hundred bytes of content), which is a mixed blessing...)