Download a File to the Client Browser using Response.WriteFile - October 2008

I found a handy tip on the web, written by Marco Bellinaso, that shows how to cause a file to be downloaded. The tip shows how to use the Server and Response objects to perform the download. So why not just provide the URL to that web page and forget about making this a tip?

Marco's Tip URL: http://www.devx.com/vb2themax/Tip/19388

Because the tip says you must include the code in a page in order to access the Server and Response objects. This restriction can be eliminated by using HttpContext.Current to prefix the objects. For example:

HttpContext.Current.Response.WriteFile(oTargetFile.FullName)

I modified Marco's code and put it into a class module. A typical calling sequence is:

Dim oDF As New cDownloadFile
oDF.DoDownloadFile(c:\MyPhotos\LarryAtWhistler_50x60.jpg)

Which will open a dialog similar to:


IE7/IE8


IE9

Download VB.Net Code

Click here to download the VS2005 VB.Net source code [1k].

Previous Tip   Tip Index   Next Tip

Home

Updated 2011/04/22