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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:45:30+00:00 2026-05-13T14:45:30+00:00

I’ve created a page that allows users to download a file when they click

  • 0

I’ve created a page that allows users to download a file when they click a button… the button’s onclick event is linked to the following chunk of code:

this.Page.Response.Clear();
this.Page.Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Zip;
this.Page.Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + System.IO.Path.GetFileName(filename) + "\"");
this.Page.Response.TransmitFile(filename);
this.Page.Response.Flush();
this.Page.Response.End();

The download works fine, but now when I try to interact with the page, (for instance, hit the download button again), nothing posts back.

Am I responding to the download request incorrectly (should I be using a different/new response object), or is there something else I need to do to make the page active after the download?


Edit:

So I’ve tried encorporating the two posters suggestions for creating a httphandler, and calling a Response.Redirect to the handler from the button’s click event.

    void submitButton_Click(object sender, EventArgs e)
    {
        label.Text = "Boo!";
        this.Page.Response.Redirect("~/ViewAttachment.ashx?id=foo", false);
    }

If I step through this on the debugger, it continues after the redirect call, but the page just returns to a state where the button doesn’t work, and the labels have their default values. Am I now doing the redirect wrong?

  • 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-13T14:45:30+00:00Added an answer on May 13, 2026 at 2:45 pm

    If using another handler an option?

    Here’s a slimmed down version of what I’ve used before (suggestions welcome this was a quick write). The HttpHandler (AttachmentFile is just a class with the blob data and some attributes collected when the file was uploaded):

    public class AttachmentHandler : IHttpHandler
    {
      public const string QueryKeyID = "ID";
    
      public void ProcessRequest(HttpContext context)
      {    
        var r = context.Response;
        var attachmentID = context.Request.QueryString[QueryKeyID];
    
        Attachment a = DataContext.GetById<AttachmentFile>(attachmentID);
    
        r.ContentType = a.ContentType;
        r.AppendHeader("Content-Type", a.ContentType);
        r.AppendHeader("content-disposition", string.Format("attachment; filename=\"{0}{1}\"", a.AttachmentName, GetExtension(a.FileName)));
        r.BufferOutput = false; //Stream the content to the client, no need to cache entire streams in memory...
        r.BinaryWrite(a.BlobData);
        r.End();
      }
    
      private static string GetExtension(string fileName)
      {
        if(fileName.IsNullOrEmpty()) return string.Empty;
        var i = fileName.LastIndexOf(".");
        return i > 0 ? fileName.Substring(i) : string.Empty;
      }
    }
    

    In the web.config:

    <system.web>
      <httpHandlers>
        <add verb="*" path="ViewAttachment.ashx" type="MyNamespace.AttachmentHandler, MyWebDllName"/>
      </httpHandlers>
    </system.web>
    

    Then all you do is render a link in the form of ~/ViewAttachment.ashx?ID=5 on the page, clicking the button will download the file, but not mess with your page’s lifecycle at all.

    Now there are more considerations, like security and such….I trimmed them out for this example. I’m also using this style of link: ~/Attachment/{id} via webforms routing…if either of these interest you I’ll update to include them. Or you could hate this approach all-together…just letting you know it’s an option.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.