The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Download a file with a browser, the server side part

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter van Ooijen

Posts: 284
Nickname: petergekko
Registered: Sep, 2003

Peter van Ooijen is a .NET devloper/architect for Gekko Software
Download a file with a browser, the server side part Posted: Mar 31, 2006 6:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter van Ooijen.
Original Post: Download a file with a browser, the server side part
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter van Ooijen
Latest Posts From Peter's Gekko

Advertisement

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.

Share this post: Email it! | bookmark it! | digg it! | reddit!

Read: Download a file with a browser, the server side part

Topic: Microsoft BizTalk Server 2006 RTMs Previous Topic   Next Topic Topic: Local variables: scope vs. lifetime (plus closures)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use