Expert advice needed!


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
 
Ad

Advertisements

Ad

Advertisements

M

Mark Rae [MVP]

The more i got into this, the more i see that it does not solve my problem
anyway. This solution seems only to apply to file system files. I do not
have a file on disk. I have my data in memory in a data table.

I use the Chilkat Zip component for this type of thing.

It has a method specifically designed to solve your particular problem:
http://www.chilkatsoft.com/refdoc/csZipRef.html#method033

Incidentally, it also has full GZip support, though you don't need that in
this instance...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top