By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,087 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,087 IT Pros & Developers. It's quick & easy.

Response with file attached make save/open dialog appear twice

P: n/a
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("conten t-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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


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

--
Daniel Fisher(lennybacon)
MCP ASP.NET C#
Blog: http://www.lennybacon.com/
"Dorte" <Do***@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
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("conten t-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

Nov 18 '05 #2

P: n/a
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

"Daniel Fisher(lennybacon)" wrote:
What about adding a ContentType to the Respone for your .csv
(text/comma-separated-values)

--
Daniel Fisher(lennybacon)
MCP ASP.NET C#
Blog: http://www.lennybacon.com/
"Dorte" <Do***@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
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("conten t-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


Nov 18 '05 #3

P: n/a
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" <Do***@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
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("conten t-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

Nov 18 '05 #4

P: n/a
Great! It works!
Thanks a lot, Bill.

Dorte

"William F. Robertson, Jr." wrote:
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" <Do***@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
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("conten t-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


Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.