Peter's Gekko

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Download a file with a browser, the server side part

I have an asp.net application where the user can download a file by clicking a link. It could be a file of any type. Getting it work took more effort than expected. Let me use my blog as a public notepad and share the result.

protected void LinkButtonDownLoad_Click(object sender, EventArgs e)

{

    string bestandsnaam = (sender as LinkButton).CommandArgument;

    string fullFileName = Server.MapPath("DownLoads") + "\\" + bestandsnaam;

    if (System.IO.File.Exists(fullFileName))

    {

        bestandsnaam =  Server.UrlPathEncode(bestandsnaam);

        Response.Clear();

        Response.ContentType = "application/binary";

        Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", bestandsnaam));

        Response.TransmitFile(fullFileName);

        Response.Flush();

        Response.End();

    }

}

The name of the file (bestandsnaam in Dutch:) is read from the linkbutton's CommandArgument as that's a bindable property. UrlPathEncode is necessary for filenames containing blanks. The ContentType is set to application/binary to make sure any format will get across safely.

Thanks to me neighbor who used to blog and is still an invaluable resource. The code is no big deal but we had to assemble it ourselves being unable to find a full working example.


Posted 03-31-2006 9:11 AM by pvanooijen
Filed under:

[Advertisement]

Comments

Jason Haley wrote Interesting Finds
on 04-01-2006 6:30 AM
Jason Haley wrote Interesting Finds
on 04-01-2006 6:34 AM
Peter's Gekko wrote The outlook calendar, iCalendar and RFC 2445
on 01-17-2007 8:19 AM

You can exchange Outlook appointments with other applications. One way is to export or import files in

The Original .NET Geek wrote The outlook calendar, iCalendar and RFC 2445
on 01-29-2007 11:38 AM

So I have been using the google calendar for some time now since I let google manage my domains . One

Add a Comment

(required)  
(optional)
(required)  
Remember Me?