Solved

CSV via ASP on a Secure Server

Posted on 2007-03-20
16
311 Views
Last Modified: 2013-12-08
Trying to create a CVS file with ASP works beautifully except when it is on a secure site “https://” and trying to download with IE. All other browsers work well, using “http://” works well. But most people use IE and there is sensitive data so it needs the secure connection.

I get this error
Internet Explorer cannot download ExportReport.asp from Doman.Name.com.
Internet Explorer was not able to open this internet site. The requested site is either unavailable or cannot be found. Please thy again later.

The offending code is
Response.ContentType="application/CSV"

I have tried
Response.ContentType="application/vnd.ms-excel"
With the same result

Also tried but
Response.ContentType="application/octet-stream"
It just opens the results as a page and not in a spreadsheet
0
Comment
Question by:cccsatlit
  • 8
  • 7
16 Comments
 

Author Comment

by:cccsatlit
Comment Utility
Also get the same error when the url for the ExportReport.asp is typed directly into the address bar (with both http:// and https://)
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
When you set the response mime type to something the user's browser has a mapping for, the browser will try to pass the stream to the application for which there is a matching mime type.

In this case you want a file to be downloaded, so you want to tell the browser it is receiving   a download.  So you tell the browser what the disposition should be rather than letting it decide.

So after your

Response.ContentType="application/CSV"
Response.AddHeader "Content-Disposition","attachment; filename=" & "myFile.csv"

That should force the download dialog.

Regards
Rod
0
 

Author Comment

by:cccsatlit
Comment Utility
I have already tried the attachment in both the ContentType and AddHeader always the same result.
Also the dialog box does open. I just get the error right after, not even given a chance to select save or open.
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
That clarifies the problem, yet it doesn't bring to mind any solution.  Sorry.

Is it possible for you to paste more code.  If you are getting the attachment save dialoge opening and then are getting an error, it could be something before or after what you've posted.
0
 

Author Comment

by:cccsatlit
Comment Utility
crlf = chr(13) & chr(10)
dlim = ","
CsvOut = ""
bdate=Request("bdate")
edate=Request("edate")
rtype=Request("rtype")
firm_code=session("firm_code")
Select Case "all"
      Case "all"
            All_Report()
            Response.ContentType="application/CSV;"
            Response.AddHeader "Content-Disposition", "attachment; filename=AllReport.csv;"
'a few more cases with the same Structure
End Select
Response.Write(CsvOut)

All_Report builds the CvsOut string
the ContentType has had many different spots from the absolute beginning to its current spot right before the write.
0
 

Author Comment

by:cccsatlit
Comment Utility
Also have this to (missed it earlier because of its location)
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
This works...

<%
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
 
'Open a stream
objStream.Type = 2 ' adTypeText
objStream.Open
objStream.LoadFromFile Server.MapPath("/ee/test.csv")
objStream.Charset = "iso-8859-1"


Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
objStream.position=2
Response.write objStream.ReadText
objStream.Flush
objStream.Close
%>
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
This also works.

<%
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
 
'Open a stream
objStream.Type = 2 ' adTypeText
objStream.Open
objStream.Charset = "iso-8859-1"
objStream.WriteText "First,Last", 1
objStream.WriteText "John,Public", 1
objStream.WriteText "John,Wayne", 1
objStream.WriteText "John,Paul", 1


Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
' point the stream to the begining or you get no output
objStream.Position=0
Response.write objStream.ReadText
objStream.Flush
objStream.Close
%>

Bottom line, it is an IE bug, your original code would work in FireFox and Opera
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:cccsatlit
Comment Utility
it works but it opens it up in the browser
i get the same result with
Response.ContentType="application/octet-stream"
i am sorry it seems that i only implied it earlier instead of stating it out right i want it to open excel or associated program.
(but the way it is looking right now i may not git it)
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
http://www.rodsdot.com/ee/streamCSV.asp

Should allow you to open in Excel in the browser or save the file.

0
 
LVL 16

Expert Comment

by:OliWarner
Comment Utility
In FFox that just asks me to save it. Same in IE7. I've got Office and OpenOffice installed but notepad seems to be the default for CSV here.
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
Obviously if Excel isn't installed and associated with the browsers csv MIME type then it should open in another program.  Thanks for checking Oli.
0
 

Author Comment

by:cccsatlit
Comment Utility
With the first one the file would have to be created all ready correct?
With the second one it may be a local setting I do get the dialog box but it then opens in the browser (it could be a company setting as it does that in both ie and ff)
(Maybe I did something wrong in my test, because yours worked well. Witch method did you use?)
But that doesn't matter I found it. Thanks to your statement that it is a bug in ie.
I looked on the Microsoft site and found that when the CacheControl is set to no-cache it will throw an error when behind an ssl.
http://support.microsoft.com/kb/323308/en-us
Their fix will fix the browser witch I can’t do but I can remove the CacheControl and now it works
Thanks for your help
I want to get your response to my questions so I will leave this open. (For a little bit)
But rdivilbiss don’t worry you will get the points
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 500 total points
Comment Utility
>>With the first one the file would have to be created all ready correct?

objStream.LoadFromFile Server.MapPath("/ee/test.csv") means the file already exists ans we read it into the stream object.

>>With the second one it may be a local setting I do get the dialog box but it then opens in the browser (it could be a company setting as it does that in both ie and ff)

It depends in part on your local settings, e.g. if the CSV mime type is set to be opened by your browser, then yes it would open in the browser.

>>(Maybe I did something wrong in my test, because yours worked well. Witch method did you use?)

<%
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
 
'Open a stream
objStream.Type = 2 ' adTypeText
objStream.Open
' objStream.LoadFromFile Server.MapPath("/ee/test.csv")
objStream.Charset = "iso-8859-1"
objStream.WriteText "First,Last", 1
objStream.WriteText "John,Public", 1
objStream.WriteText "John,Wayne", 1
objStream.WriteText "John,Paul", 1

Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
objStream.Position=0
Response.write objStream.ReadText
objStream.Flush
objStream.Close
%>

This is the entire code for the test page I posted that worked well.

NOTE: I used Response.ContentType = "application/vnd.ms-excel" to allow it to be opened in Excel, if loaded on the machine, but

Response.AddHeader "Content-Disposition","attachment; filename=test.csv"
should always force the dialog to allow the browser to choose save or open.

>>I looked on the Microsoft site and found that when the CacheControl is set to no-cache it will throw an error when behind an ssl.  http://support.microsoft.com/kb/323308/en-us

Yes, well you don't really want that set anyway for a download, but I agree its an IE bug, re SSL.

>>Thanks for your help

Your welcome.

>>I want to get your response to my questions so I will leave this open. (For a little bit)

Here they are.
Happy Programming!
0
 

Author Comment

by:cccsatlit
Comment Utility
thanks again
and have fun
0
 
LVL 29

Expert Comment

by:rdivilbiss
Comment Utility
You're Welcome
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now