Solved

Using the wizard control - cannot use Response.Write

Posted on 2007-11-27
7
622 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

726 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