Solved

Using the wizard control - cannot use Response.Write

Posted on 2007-11-27
7
613 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now