Response with file attached make save/open dialog appear twice

G

Guest

Hi,

I am using the code below to stream a CSV file with the response object.

Dim FileName As String = "Test.csv"
With Web.HttpContext.Current.Response

Web.HttpContext.Current.Response.AddHeader("content-disposition",
"attachment; filename=" & FileName)
.Charset = ""
.Write(Output.ToString()) ' Output is a StringBuilder object
with the content to write
.End()
End With

But when this code has been executed, a dialog pops up asking the user
whether to save or open the file. If the user clicks "Open", the dialog pops
up again. After cliking "Open" in the second dialog, the file is opened by
Excel. If the user clicks "Save", the file is saved, and the dialog has only
appeared once.
In the first dialog "Always ask before opening this type of file" is grayed
out - in the second it is enabled, and checked.
Why does the dialog show up twice? Has it something to do with the browser
settings, or what am I doing wrong? I would appreciate any kind of help.

Thanks,
Dorte
 
D

Daniel Fisher\(lennybacon\)

What about adding a ContentType to the Respone for your .csv
(text/comma-separated-values)
 
G

Guest

Doesn't seem to work....
I tried adding the line
Web.HttpContext.Current.Response.ContentType = "text/comma-separated-values"
but it doesn't make any difference.

Dorte
 
W

William F. Robertson, Jr.

Here is the code I use to stream a csv and it opens in excel. This is in my
button click event. I might be doing extra steps, but I know for sure this
works. I create a temporary file, stream the text ( probably your
Output.ToString() ) to this file, then open the file up and read the bytes
then using BinaryWrite to send the bytes to the browser.

You could probably just skip the temp file and convert your string to bytes.

string filename = "Name Of Report.csv";
byte [] bytes = Read file bytes.

Response.ClearHeaders();
Response.Clear();
Response.AddHeader( "Content-Disposition", "inline; filename=" +
filename );
Response.ContentType = "text/csv";
Response.BinaryWrite( bytes );
Response.End();

HTH,

bill
 
G

Guest

Great! It works!
Thanks a lot, Bill.

Dorte

William F. Robertson said:
Here is the code I use to stream a csv and it opens in excel. This is in my
button click event. I might be doing extra steps, but I know for sure this
works. I create a temporary file, stream the text ( probably your
Output.ToString() ) to this file, then open the file up and read the bytes
then using BinaryWrite to send the bytes to the browser.

You could probably just skip the temp file and convert your string to bytes.

string filename = "Name Of Report.csv";
byte [] bytes = Read file bytes.

Response.ClearHeaders();
Response.Clear();
Response.AddHeader( "Content-Disposition", "inline; filename=" +
filename );
Response.ContentType = "text/csv";
Response.BinaryWrite( bytes );
Response.End();

HTH,

bill

Dorte said:
Hi,

I am using the code below to stream a CSV file with the response object.

Dim FileName As String = "Test.csv"
With Web.HttpContext.Current.Response

Web.HttpContext.Current.Response.AddHeader("content-disposition",
"attachment; filename=" & FileName)
.Charset = ""
.Write(Output.ToString()) ' Output is a StringBuilder object
with the content to write
.End()
End With

But when this code has been executed, a dialog pops up asking the user
whether to save or open the file. If the user clicks "Open", the dialog pops
up again. After cliking "Open" in the second dialog, the file is opened by
Excel. If the user clicks "Save", the file is saved, and the dialog has only
appeared once.
In the first dialog "Always ask before opening this type of file" is grayed
out - in the second it is enabled, and checked.
Why does the dialog show up twice? Has it something to do with the browser
settings, or what am I doing wrong? I would appreciate any kind of help.

Thanks,
Dorte
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top