Solved

Using the wizard control - cannot use Response.Write

Posted on 2007-11-27
7
617 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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