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. Create a text file and download it ! [modified]

Create a text file and download it ! [modified]

Scheduled Pinned Locked Moved ASP.NET
csharptutorialquestion
7 Posts 4 Posters 0 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.
  • M Offline
    M Offline
    Merlin Tintin
    wrote on last edited by
    #1

    Hi ! Here is what I would like to do.. User clicks on a button. I want to create a report in text mode (generated by my ASP .NET application - C#) and then show a download box on the browser of the user to allow him to download this report. The filename of the report will be defined previously. I have delete all header from my aspx page to just leave this <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageName.aspx.cs" Inherits="PageName" %> and in my code behind i do: Response.Write ("xxx"); How to show the download box on the screen ? Thanks ! -- modified at 3:09 Friday 21st September, 2007 -- modified at 4:15 Friday 21st September, 2007

    La Richesse & la Gloire ne griseront jamais que les temples

    I S P 3 Replies Last reply
    0
    • M Merlin Tintin

      Hi ! Here is what I would like to do.. User clicks on a button. I want to create a report in text mode (generated by my ASP .NET application - C#) and then show a download box on the browser of the user to allow him to download this report. The filename of the report will be defined previously. I have delete all header from my aspx page to just leave this <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageName.aspx.cs" Inherits="PageName" %> and in my code behind i do: Response.Write ("xxx"); How to show the download box on the screen ? Thanks ! -- modified at 3:09 Friday 21st September, 2007 -- modified at 4:15 Friday 21st September, 2007

      La Richesse & la Gloire ne griseront jamais que les temples

      I Offline
      I Offline
      Imran Khan Pathan
      wrote on last edited by
      #2

      cREATE .TXT FILE DYNAMICALLY, WRITE CONTENT AND SAVE IT IN THE SERVER. YOU CAN EASILY DOWNLOAD FROM THERE bEST rEGARD pATHAN

      ---------------------------------------------------

      1 Reply Last reply
      0
      • M Merlin Tintin

        Hi ! Here is what I would like to do.. User clicks on a button. I want to create a report in text mode (generated by my ASP .NET application - C#) and then show a download box on the browser of the user to allow him to download this report. The filename of the report will be defined previously. I have delete all header from my aspx page to just leave this <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageName.aspx.cs" Inherits="PageName" %> and in my code behind i do: Response.Write ("xxx"); How to show the download box on the screen ? Thanks ! -- modified at 3:09 Friday 21st September, 2007 -- modified at 4:15 Friday 21st September, 2007

        La Richesse & la Gloire ne griseront jamais que les temples

        S Offline
        S Offline
        Sandeep Akhare
        wrote on last edited by
        #3

        As Pathan said create a file at the server and store that file in one of the folders of your application and then download it bu using this code string filename = "YourFileName.txt"; if (filename != "") { string path = Server.MapPath(filename); System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); }

        Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

        M 1 Reply Last reply
        0
        • M Merlin Tintin

          Hi ! Here is what I would like to do.. User clicks on a button. I want to create a report in text mode (generated by my ASP .NET application - C#) and then show a download box on the browser of the user to allow him to download this report. The filename of the report will be defined previously. I have delete all header from my aspx page to just leave this <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageName.aspx.cs" Inherits="PageName" %> and in my code behind i do: Response.Write ("xxx"); How to show the download box on the screen ? Thanks ! -- modified at 3:09 Friday 21st September, 2007 -- modified at 4:15 Friday 21st September, 2007

          La Richesse & la Gloire ne griseront jamais que les temples

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

          There are already articles[^]on CodeProject that tell you how to do this. Did you check before you posted? Paul

          M 1 Reply Last reply
          0
          • S Sandeep Akhare

            As Pathan said create a file at the server and store that file in one of the folders of your application and then download it bu using this code string filename = "YourFileName.txt"; if (filename != "") { string path = Server.MapPath(filename); System.IO.FileInfo file = new System.IO.FileInfo(path); if (file.Exists) { Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/octet-stream"; Response.WriteFile(file.FullName); Response.End(); } else { Response.Write("This file does not exist."); }

            Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

            M Offline
            M Offline
            Merlin Tintin
            wrote on last edited by
            #5

            Thank you Sandeep for your answer. 1. unfotunately your code does not work... this condition is never true: if (file.Exists) => i modify your code and it's working... if (filename != "") { string path = Server.MapPath ( filename ); System.IO.FileInfo file = new System.IO.FileInfo ( path ); StreamWriter fp = File.CreateText ( path ); fp.WriteLine(text); fp.Close (); if ( file.Exists ) { Response.Clear (); Response.AddHeader ( "Content-Disposition", "attachment; filename=" + file.Name ); Response.AddHeader ( "Content-Length", file.Length.ToString () ); Response.ContentType = "application/octet-stream"; Response.WriteFile ( file.FullName ); Response.End (); } else { Response.Write("This file does not exist."); } } 2. I'm certain there is a way to show the download box without writing the file on the server. Thanks !

            La Richesse & la Gloire ne griseront jamais que les temples

            S 1 Reply Last reply
            0
            • P pmarfleet

              There are already articles[^]on CodeProject that tell you how to do this. Did you check before you posted? Paul

              M Offline
              M Offline
              Merlin Tintin
              wrote on last edited by
              #6

              No i didn't found it ! Thanks a lot !!!

              La Richesse & la Gloire ne griseront jamais que les temples

              1 Reply Last reply
              0
              • M Merlin Tintin

                Thank you Sandeep for your answer. 1. unfotunately your code does not work... this condition is never true: if (file.Exists) => i modify your code and it's working... if (filename != "") { string path = Server.MapPath ( filename ); System.IO.FileInfo file = new System.IO.FileInfo ( path ); StreamWriter fp = File.CreateText ( path ); fp.WriteLine(text); fp.Close (); if ( file.Exists ) { Response.Clear (); Response.AddHeader ( "Content-Disposition", "attachment; filename=" + file.Name ); Response.AddHeader ( "Content-Length", file.Length.ToString () ); Response.ContentType = "application/octet-stream"; Response.WriteFile ( file.FullName ); Response.End (); } else { Response.Write("This file does not exist."); } } 2. I'm certain there is a way to show the download box without writing the file on the server. Thanks !

                La Richesse & la Gloire ne griseront jamais que les temples

                S Offline
                S Offline
                Sandeep Akhare
                wrote on last edited by
                #7

                Do one thing then get all content of your file in stream and pass that stream in this method hope that will help By the way whats the problem in creating a file ?

                Thanks and Regards Sandeep If If you look at what you do not have in life, you don't have anything, If you look at what you have in life, you have everything... "

                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