Expert advice needed!

Discussion in 'ASP .Net' started by Smith, Nov 9, 2007.

  1. Smith

    Smith Guest

    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
     
    Smith, Nov 9, 2007
    #1
    1. Advertisements


  2. here's a discussion of your issue:

    http://msdn.microsoft.com/msdnmag/issues/05/10/NETMatters/
     
    Barrie Wilson, Nov 9, 2007
    #2
    1. Advertisements

  3. Smith

    Smith Guest

    Smith, Nov 9, 2007
    #3
  4. Smith

    Smith Guest

    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.

    Any furthere thoughts?

    Thanks
    Smith
     
    Smith, Nov 9, 2007
    #4
  5. 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...
     
    Mark Rae [MVP], Nov 9, 2007
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.