Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:56:49+00:00 2026-05-28T22:56:49+00:00

I have an *.ashx (handler) with this code: public void ProcessRequest (HttpContext context) {

  • 0

I have an *.ashx (handler)
with this code:

public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/zip";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=catalog" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip");

    // magic happends here to get a DataTable called 'dt'

    using (ZipFile zip = new ZipFile(Encoding.UTF8))
    {
        foreach (DataRow dr in dt.Rows)
        {
            string barCode = "C:/tmp/bc/" + dr["ProductCode"] + ".gif";

            if (File.Exists(barCode))
            {
                if (!zip.EntryFileNames.Contains("bc" + dr["ProductCode"] + ".gif"))
                {
                    try
                    {
                        // this option does not work
                        using (StreamReader sr = new StreamReader(barCode))
                        {
                            if (sr.BaseStream.CanRead)
                                zip.AddEntry("bc" + dr["ProductCode"] + ".gif", sr.BaseStream);
                        }
                        // but the next line does work... WHY?
                        zip.AddEntry("bc" + dr["ProductCode"] + ".gif", File.ReadAllBytes(barCode));
                    }
                    catch (Exception ex)
                    {
                        // never hits the catch
                        context.Response.Write(ex.Message);
                    }
                }
            }
        }
        zip.Save(context.Response.OutputStream); // here is the exception if I use the first option
    }
}

I use the latest version of http://dotnetzip.codeplex.com/
Can someone explain to me, why the File.ReadAllBytes does work and the StreamReader does crash when saving to the OutputStream?
The exception message is Cannot access a closed file

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:56:51+00:00Added an answer on May 28, 2026 at 10:56 pm

    The problem is that you wrap the stream in a using statement. At the end of the using statement the stream is disposed.

    When you call zip.save the library tries to access a stream which has been closed. File.ReadAllBytes will not fail because it passes the data directly.

    public void ProcessRequest (HttpContext context) {
    context.Response.ContentType = "application/zip";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=catalog" + DateTime.Now.ToString("yyyy-MM-dd") + ".zip");
    
        using (ZipFile zip = new ZipFile(Encoding.UTF8))
        {
            foreach (DataRow dr in dt.Rows)
            {
                string barCode = "C:/tmp/bc/" + dr["ProductCode"] + ".gif";
    
                if (File.Exists(barCode))
                {
                    if (!zip.EntryFileNames.Contains("bc" + dr["ProductCode"] + ".gif"))
                    {
                        try
                        {
                            // The file stream is opened here
                            using (StreamReader sr = new StreamReader(barCode))
                            {
                                if (sr.BaseStream.CanRead)
                                    zip.AddEntry("bc" + dr["ProductCode"] + ".gif", sr.BaseStream);
                            }
                            // The file stream is closed here
                        }
                        catch (Exception ex)
                        {
                            // never hits the catch
                            context.Response.Write(ex.Message);
                        }
                    }
                }
            }
    
            // The closed file streams are accessed here
            zip.Save(context.Response.OutputStream);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ASHX HTTPHandler which implements IRequiresSessionState. When I call this handler, and call
I have a Generic HTTP Handler (*.ashx) in my ASP.Net application which performs some
A simple problem. I have an ASHX handler which generates a report. Unfortunately, this
I have created an ashx handler that returns an image to my flex app.
Is it possible to call a handler using javascript code? e.g. i have a
I have an ASHX handler that I am using to display images that are
I have made a generic handler (.ashx) to fetch some database values using jquery
I have created an ashx handler to render image thumbnails from images in a
Is it possible to get a web resource from an .ashx handler? We have
I have an ASHX handler returning an image on page load. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.