Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Code for Downloading from website problem

Code for Downloading from website problem

Scheduled Pinned Locked Moved ASP.NET
csharpjavascripthtmlalgorithmstools
5 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file. I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file. When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML. The download method is:

    public void ExportCSV(DataTable data, string fileName)
    {
    try
    {
    HttpContext context = HttpContext.Current;

        context.Response.Clear();
        context.Response.ContentType = "text/csv";
        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
    
        //write column header names
        for (int i = 0; i < data.Columns.Count - 1; i++)
        {
            if (i > 0)
            {
                context.Response.Write(",");
            }
            context.Response.Write(data.Columns\[i\].ColumnName);
        }
        context.Response.Write(Environment.NewLine);
    
        //Write data
        foreach (DataRow row in data.Rows)
        {
    
            for (int i = 0; i < data.Columns.Count - 1; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(row\[i\]);
            }
            context.Response.Write(Environment.NewLine);
        }
    
        context.Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    

    }

    Here is part of the file that is downloaded:

    HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
    RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
    90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
    90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
    90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
    900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
    90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
    <script type="text/javascript" language="javascript">alert('Thread was being a

    V N P K 4 Replies Last reply
    0
    • K kbalias

      Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file. I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file. When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML. The download method is:

      public void ExportCSV(DataTable data, string fileName)
      {
      try
      {
      HttpContext context = HttpContext.Current;

          context.Response.Clear();
          context.Response.ContentType = "text/csv";
          context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
      
          //write column header names
          for (int i = 0; i < data.Columns.Count - 1; i++)
          {
              if (i > 0)
              {
                  context.Response.Write(",");
              }
              context.Response.Write(data.Columns\[i\].ColumnName);
          }
          context.Response.Write(Environment.NewLine);
      
          //Write data
          foreach (DataRow row in data.Rows)
          {
      
              for (int i = 0; i < data.Columns.Count - 1; i++)
              {
                  if (i > 0)
                  {
                      context.Response.Write(",");
                  }
                  context.Response.Write(row\[i\]);
              }
              context.Response.Write(Environment.NewLine);
          }
      
          context.Response.End();
      }
      catch (Exception ex)
      {
          throw ex;
      }
      

      }

      Here is part of the file that is downloaded:

      HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
      RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
      90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
      90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
      90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
      900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
      90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
      <script type="text/javascript" language="javascript">alert('Thread was being a

      V Offline
      V Offline
      Viral Upadhyay
      wrote on last edited by
      #2

      it look like some think crashing while you get data and as a result it is giving you this problem. Check all the value from DB.

      Viral My Blog Save Our Tigers

      1 Reply Last reply
      0
      • K kbalias

        Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file. I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file. When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML. The download method is:

        public void ExportCSV(DataTable data, string fileName)
        {
        try
        {
        HttpContext context = HttpContext.Current;

            context.Response.Clear();
            context.Response.ContentType = "text/csv";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
        
            //write column header names
            for (int i = 0; i < data.Columns.Count - 1; i++)
            {
                if (i > 0)
                {
                    context.Response.Write(",");
                }
                context.Response.Write(data.Columns\[i\].ColumnName);
            }
            context.Response.Write(Environment.NewLine);
        
            //Write data
            foreach (DataRow row in data.Rows)
            {
        
                for (int i = 0; i < data.Columns.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        context.Response.Write(",");
                    }
                    context.Response.Write(row\[i\]);
                }
                context.Response.Write(Environment.NewLine);
            }
        
            context.Response.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        

        }

        Here is part of the file that is downloaded:

        HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
        RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
        90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
        90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
        90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
        900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
        90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
        <script type="text/javascript" language="javascript">alert('Thread was being a

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        You should not be using Response.End. Rather use Flush and/or CompleteRequest "This method is provided only for compatibility with ASP—that is, for compatibility with COM-based Web-programming technology that preceded ASP.NET. If you want to jump ahead to the EndRequest event and send a response to the client, call CompleteRequest() instead." http://msdn.microsoft.com/en-us/library/system.web.httpresponse.end.aspx[^]


        I know the language. I've read a book. - _Madmatt

        1 Reply Last reply
        0
        • K kbalias

          Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file. I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file. When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML. The download method is:

          public void ExportCSV(DataTable data, string fileName)
          {
          try
          {
          HttpContext context = HttpContext.Current;

              context.Response.Clear();
              context.Response.ContentType = "text/csv";
              context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
          
              //write column header names
              for (int i = 0; i < data.Columns.Count - 1; i++)
              {
                  if (i > 0)
                  {
                      context.Response.Write(",");
                  }
                  context.Response.Write(data.Columns\[i\].ColumnName);
              }
              context.Response.Write(Environment.NewLine);
          
              //Write data
              foreach (DataRow row in data.Rows)
              {
          
                  for (int i = 0; i < data.Columns.Count - 1; i++)
                  {
                      if (i > 0)
                      {
                          context.Response.Write(",");
                      }
                      context.Response.Write(row\[i\]);
                  }
                  context.Response.Write(Environment.NewLine);
              }
          
              context.Response.End();
          }
          catch (Exception ex)
          {
              throw ex;
          }
          

          }

          Here is part of the file that is downloaded:

          HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
          RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
          90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
          90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
          90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
          900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
          90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
          <script type="text/javascript" language="javascript">alert('Thread was being a

          P Offline
          P Offline
          Prasanta_Prince
          wrote on last edited by
          #4

          File Upload and Download in ASP.NET[^] This link shows Upload and download.

          1 Reply Last reply
          0
          • K kbalias

            Hello I am using Visual Web Developer 2010 Express (with C# as code-behind) to develop a website. On one of the pages the user can create a report of the data and I would like the user to be able to download the data as a .csv file. I have written the method to download the data, but the HTML of the page is appended to the data in the downloaded file. When searching for solutions people say the Response.Clear() and Response.End() must be used in the code, but I have that and it still appends the HTML. The download method is:

            public void ExportCSV(DataTable data, string fileName)
            {
            try
            {
            HttpContext context = HttpContext.Current;

                context.Response.Clear();
                context.Response.ContentType = "text/csv";
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");
            
                //write column header names
                for (int i = 0; i < data.Columns.Count - 1; i++)
                {
                    if (i > 0)
                    {
                        context.Response.Write(",");
                    }
                    context.Response.Write(data.Columns\[i\].ColumnName);
                }
                context.Response.Write(Environment.NewLine);
            
                //Write data
                foreach (DataRow row in data.Rows)
                {
            
                    for (int i = 0; i < data.Columns.Count - 1; i++)
                    {
                        if (i > 0)
                        {
                            context.Response.Write(",");
                        }
                        context.Response.Write(row\[i\]);
                    }
                    context.Response.Write(Environment.NewLine);
                }
            
                context.Response.End();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            

            }

            Here is part of the file that is downloaded:

            HospNumber,LastName,FirstName,DOB,Gender,RaceName,DateDied,AgeAtDeath,HospShort,RTID,
            RTNum,RegistrationDate,AgeAtRegDate,OSurvival,Last seen,Next appt,Firm,Clinic,Organ,Part,Histology,Intent,Modality,StGp1,StGp2,T,N,M,Study - Other
            90000000,Temp,,18/04/1955 12:00:00 AM,,,,,GSH,61227,2010.1103,14/01/2010 12:00:00 AM,,-1,,,,,,,,,,,,,,,
            90001245,TestWeb,,23/02/1983 12:00:00 AM,,,23/06/2011 12:00:00 AM,028,04,00,GSH,,,,,,,,,,,,,,,,,,,,
            90003214,TestWeb,,18/01/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
            900223341,TestWeb,,23/03/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
            90023541,TestWeb,,23/02/1983 12:00:00 AM,,,,,GEO,,,,,,,,,,,,,,,,,,,,
            <script type="text/javascript" language="javascript">alert('Thread was being a

            K Offline
            K Offline
            kbalias
            wrote on last edited by
            #5

            Hi I managed to get this to work. I originally had the code in a try...catch block. When I removed the try...catch it worked perfectly. I cannot explain that. Thanks for all the replies. Kobus

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups