ASP.Net/VB.Net - How to prompt download of a file in physical path on server?

Posted on 2008-11-19
Last Modified: 2013-11-05
I have zip files stored in a path that is in a physical path referenced in my web config.  I can't use a virtual directory because we will be on a web farm.  They are zip files that I will have the name in a datagrid which I want to be a linkbutton that can prompt the download of the file for the client machine.  Any idea how I can do that.
Question by:sbornstein2
    LVL 16

    Expert Comment

    Create a generic HttpHandler and open the file for read within that and write it to the response stream. Your link button would then point to something ilke "MyHttpHandler.ashx?FileID=123".
    LVL 22

    Accepted Solution

    Alternatively, you can use Response stream to force the file download dialog to display. Here is how:
    1. Make sure ASPNET has read access to the file or folder.
    2. In the event handler that handles the click of the LinkButton, do something like this (assume you already retrieved the file path from web.config file):

    Dim fi As System.IO.FileInfo = New FileInfo(path)
    If (fi.Exists) Then
    	Response.AddHeader("Content-Disposition", "attachment;filename=" & fi.Name)
    	Response.ContentType = "application/octstream"
    	Response.AddHeader("Content-Length", fi.Length)
    End If

    Open in new window


    Author Closing Comment

    this is perfect

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now