S
Smith
Hello,
My code below create a .CSV file and compress it with the new GZipStream
class.
My problem, is how to set the extension of the uncompressed file to .csv.
Users have to manually do this and it is not quite convenient. Any help will
be highly appreciated.
File: handler.ashx:
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
HttpRequest Request = context.Request;
Response.Clear();
Response.Buffer = true;
//Using a new compression class of the framework
GZipStream gzStream = new GZipStream(Response.OutputStream,
CompressionMode.Compress);
StreamWriter sw = new StreamWriter(gzStream);
DataTable dt = Session["MyDataTable"];
if (dt != null)
{
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr))
{
sw.Write(dr.ToString().Trim());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Response.ContentType = "application/x-Gzip";
Response.AddHeader("content-disposition", "attachment; filename=" +
"compressed_file.gz");
Response.End();
}
}
S
My code below create a .CSV file and compress it with the new GZipStream
class.
My problem, is how to set the extension of the uncompressed file to .csv.
Users have to manually do this and it is not quite convenient. Any help will
be highly appreciated.
File: handler.ashx:
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
HttpRequest Request = context.Request;
Response.Clear();
Response.Buffer = true;
//Using a new compression class of the framework
GZipStream gzStream = new GZipStream(Response.OutputStream,
CompressionMode.Compress);
StreamWriter sw = new StreamWriter(gzStream);
DataTable dt = Session["MyDataTable"];
if (dt != null)
{
int iColCount = dt.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dt.Columns);
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr))
{
sw.Write(dr.ToString().Trim());
}
if (i < iColCount - 1)
{
sw.Write(",");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
Response.ContentType = "application/x-Gzip";
Response.AddHeader("content-disposition", "attachment; filename=" +
"compressed_file.gz");
Response.End();
}
}
S