Create a text file and download it ! [modified]
-
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
-
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
cREATE .TXT FILE DYNAMICALLY, WRITE CONTENT AND SAVE IT IN THE SERVER. YOU CAN EASILY DOWNLOAD FROM THERE bEST rEGARD pATHAN
---------------------------------------------------
-
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
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... "
-
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
-
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... "
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
-
No i didn't found it ! Thanks a lot !!!
La Richesse & la Gloire ne griseront jamais que les temples
-
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
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... "