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 8144287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:20:12+00:00 2026-06-06T13:20:12+00:00

I wrote a ashx handler that streams files to the browser in a secure

  • 0

I wrote a ashx handler that streams files to the browser in a secure way, I want users to be authorised to get to these files.

Problem is that when I steam big files (+40 MB), the session is gonen + the browser download suddenly interrupts after ~40 MB.

I have web.config configured not to time-out before 240 minutes.

testing this locally doesn’t give me the same problem, testing this on my shared host does.

Anyone can point me in the right direction?

I tried with and without Reponse.Clear()

public void ProcessRequest(HttpContext context)
    {
        int id;

        if (new Core.SecurityManager().CurrentUser != null)
        {

            try
            {
                id = Convert.ToInt32(context.Request.QueryString["id"]);
            }
            catch
            {
                throw new ApplicationException("id could not  be parsed.");
            }

            string filename = new DocumentFactory().SelectDocumentById(id).Filename;

            string filePath = context.Server.MapPath("~/uploads/" + filename);

            //context.Response.Clear();

            context.Response.AddHeader("content-disposition", "attachment; filename=" + filename);

            context.Response.ContentType = "application/octet-stream";

            context.Response.WriteFile(filePath);

            //context.Response.Flush();

            //context.Response.End();
        }
        else
        {
            throw  new AuthenticationException();
        }

    }

Web.config:

 <sessionState mode="InProc" cookieless="false" timeout="240"></sessionState>

Edit tried following, but still the download interrupts:

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            byte[] byteArray = new byte[fs.Length];

            using (MemoryStream ms = new MemoryStream(byteArray))
            {
                long dataLengthToRead = ms.Length;
                int blockSize = dataLengthToRead >= 5000 ? 5000 : (int)dataLengthToRead;
                byte[] buffer = new byte[dataLengthToRead];
                context.Response.Clear();


                // Clear the content of the response
                context.Response.ClearContent();
                context.Response.ClearHeaders();


                // Buffer response so that page is sent
                // after processing is complete.
                context.Response.BufferOutput = true;


                // Add the file name and attachment,
                // which will force the open/cance/save dialog to show, to the header
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);


                // bypass the Open/Save/Cancel dialog
                //Response.AddHeader("Content-Disposition", "inline; filename=" + doc.FileName);


                // Add the file size into the response header
                context.Response.AddHeader("Content-Length", fs.Length.ToString());


                // Set the ContentType
                context.Response.ContentType = "application/octet-stream";


                // Write the document into the response
                while (dataLengthToRead > 0 && context.Response.IsClientConnected)
                {
                    Int32 lengthRead = ms.Read(buffer, 0, blockSize);
                    context.Response.OutputStream.Write(buffer, 0, lengthRead);
                    //Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }

                context.Response.Flush();
                context.Response.Close();
            }


            // End the response
            context.Response.End();

When going straight to the file through the browser by adding the full path there is no problem what so ever downloading.

  • 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-06-06T13:20:13+00:00Added an answer on June 6, 2026 at 1:20 pm

    Although correct way to deliver the big files in IIS is the following option,

    1. Set MinBytesPerSecond to Zero in WebLimits (This will certainly help in improving performance, as IIS chooses to close clients holding KeepAlive connections with smaller size transfers)

    2. Allocate More Worker Process to Application Pool, I have set to 8, now this should be done only if your server is distributing larger files. This will certainly cause other sites to perform slower, but this will ensure better deliveries. We have set to 8 as this server has only one website and it just delivers huge files.

    3. Turn off App Pool Recycling

    4. Turn off Sessions

    5. Leave Buffering On

    6. Before each of following steps, check if Response.IsClientConnected is true, else give up and dont send anything.

    7. Set Content-Length before sending the file

    8. Flush the Response

    9. Write to Output Stream, and Flush in regular intervals

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is. I'm sending values to Generic Handler via Ajax like that way.
I wrote a program that forks some processes with fork(). I want to kill
I have written a small ASHX handler that uses a small state object that
A simple problem. I have an ASHX handler which generates a report. Unfortunately, this
I am having an issue with my ASHX handler that is generating PDF. When
I'm getting the following error from the following http handler. Other ashx files are
I wrote a routine to remove pounds and ids from sharepoint fields that worked
I wrote a C program in Linux that mallocs memory, ran it in a
I wrote a trigger that updates local table and similar table on linked server.
I have an *.ashx (handler) with this code: public void ProcessRequest (HttpContext context) {

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.