Search code examples
asp.nethttp-headersimagedownload

ASP.NET Downloadable Image Files?


I don't have apache support on this website, but I need to be able to allow images to be downloadable in a certain directory only. How can I do this? This site only has ASP.NET support, and it's killing me! Noticed this link: How to download files in mvc3? but not sure where to put that code at, or even if that code there would help me any.

Any help would be greatly appreciated! A Starting point or something...

Is there a way I can just do this in HTML? Like, for example, set the download link to point to an html file, where the HTML file grabs the image file and makes it downloadable, in and of itself?

So far I have the following ASP code in a file called: default.asp

Which begins the download just fine, but it downloads an empty file (download.jpg). How can I point the following code to an actual image file to download?

<%@ Language=VBScript %>
<%  Option Explicit

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" + "download.jpg"

%>

I have a file named, "download.jpg" even within the same directory, but it never downloads the actual image. It downloads a 90 byte empty image file instead.

I even tried this with no luck:

<%@ Language=VBScript %>
<%  Option Explicit

Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition","attachment; filename=07awardee.png"
Response.TransmitFile Server.MapPath("~/images/07awardee.png")
Response.End

%>

And yes, I have the 07awardee.png file in images/07awardee.png on the server root and even in the folder root where default.asp is located. Arggg! What gives here? The file is a bit bigger now at 392 bytes, but it still isn't readable as an image file... I've been searching the internet and this is supposed to work, but doesn't! What could be the problem here?


Solution

  • OMG, I rock. Here's the way this is done. Create a file called, download.aspx, and input the following code into it:

    <%@ Page language="vb" runat="server" explicit="true" strict="true" %>
    <script language="vb" runat="server">
    Sub Page_Load(Sender As Object, E As EventArgs)
        Dim strRequest As String = Request.QueryString("file")
        If strRequest <> "" AND strRequest.EndsWith(".jpg") OR strRequest.EndsWith(".jpeg") OR strRequest.EndsWith(".png") OR strRequest.EndsWith(".gif") OR strRequest.EndsWith(".pdf") OR strRequest.EndsWith(".doc") OR strRequest.EndsWith(".docx") OR strRequest.EndsWith(".bmp") Then
            Dim path As String = Server.MapPath(strRequest)
            Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
            If file.Exists Then
                Response.Clear()
                Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
                Response.AddHeader("Content-Length", file.Length.ToString())
                Response.ContentType = "application/octet-stream"
                Response.WriteFile(file.FullName)
                Response.End
            Else
                Response.Write("This file does not exist.")
            End If
        Else
            Response.Write("You do not have permission to download this file type!")
        End If
    End Sub
    </script>
    

    Now, when you want to get a file to download (ANY FILE), just link it like so:

    <a href="download.aspx?file=/images/logo.gif">Download the logo image</a>
    

    And that's all she wrote!