Solved

Using the wizard control - cannot use Response.Write

Posted on 2007-11-27
7
618 Views
Last Modified: 2013-11-26
I have an export routine as follows that works great on an aspx web form.  Essentially a datareader is exported to a csv file with ability to download file onto desktop.
Here is the code snippet.

     using (StringWriter sw1 = new StringWriter(sb1))
                {
                    sw1.Write(thisReader.GetName(4) + "," + thisReader.GetName(5) + "," + thisReader.GetName(7));
                    while (thisReader.Read())
                    {
                        sw1.WriteLine(thisReader["FirstName"] + "," + thisReader["LastName"] + "," + thisReader["Address1"]);
                    }
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());



Now if I put this code as part of a wizard control - I don't get the popup save dialog box.  How can I fix this?
0
Comment
Question by:mugsey
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20357551
Try this:
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());
 
//try adding the following line afterwards
                    Response.End();

Open in new window

0
 

Author Comment

by:mugsey
ID: 20357888
Thanks Andrew

But this still does not work.  Any more suggestions..?
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 20358012

            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);
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 7

Expert Comment

by:Maverick_Cool
ID: 20358025
Try addin contentType

            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);
            Response.ContentType = "whatever";
             Response.AddHeader("content-disposition", "attachment;filename=" + filename);

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 20358088
try fludhing the string writer to push all the text into the StringBuilder.


     using (StringWriter sw1 = new StringWriter(sb1))
                {
                    sw1.Write(thisReader.GetName(4) + "," + thisReader.GetName(5) + "," + thisReader.GetName(7));
                    while (thisReader.Read())
                    {
                        sw1.WriteLine(thisReader["FirstName"] + "," + thisReader["LastName"] + "," + thisReader["Address1"]);
                    }
                    sw1.Flush();
                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;filename=" + filename);
                    Response.Write(sw1.ToString());

Open in new window

0
 

Author Comment

by:mugsey
ID: 20358149
Sorry guys - this still does not work.
0
 
LVL 1

Accepted Solution

by:
JoeDW earned 500 total points
ID: 20503620
Not sure you if you have solved this already, but this is the code I use and I know it works for me. Now granted it is for a file that exists on the drive already but I believe it might help
FileInfo file = new FileInfo(svrPath);
 
                if (file.Exists)
                {
                    // clear the current output content from the buffer 
                    Response.Clear();
                    // add the header that specifies the default filename for the Download/SaveAs dialog 
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    // add the header that specifies the file size, so that the browser 
                    // can show the download progress 
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    // specify that the response is a stream that cannot be read by the 
                    // client and must be downloaded 
                    Response.ContentType = "application/octet-stream";
                    // send the file stream to the client 
                    Response.WriteFile(file.FullName);
 
                    downloaded = true;
 
                }

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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