Solved

CSV via ASP on a Secure Server

Posted on 2007-03-20
16
321 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
ID: 18758204
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
ID: 18764519
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
ID: 18765349
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18766571
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
ID: 18770705
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
ID: 18778960
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
ID: 18779636
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
ID: 18779673
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
 

Author Comment

by:cccsatlit
ID: 18792557
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
ID: 18793243
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
ID: 18793393
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
ID: 18793436
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
ID: 18801896
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
ID: 18802003
>>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
ID: 18810714
thanks again
and have fun
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18810740
You're Welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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