Domanda

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?

È stato utile?

Soluzione 3

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!

Altri suggerimenti

Your .aspx page with a Request.End throws a ThreadAbortException, and that's bad for server performance (too many of these can even crash a server). So you'll want to avoid that. http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

The way I handle this problem is with a HttpHandler (.ashx) and use that to serve downloadable image files. I used some of your code because my implementation was in C# and has much more code (includes image scaling options etc):

//<%@ WebHandler Language="VB" Class="DownloadImage" %> // uncomment this line!

Public Class DownloadImage : Implements IHttpHandler
  Protected EnabledTypes() As String = New String() {".jpg", ".gif", ".png"}

  Public Sub ProcessRequest(ByVal context As HttpContext) _
    Implements IHttpHandler.ProcessRequest
    Dim request = context.Request
    If Not String.IsNullOrEmpty(request.QueryString("file")) Then
      Dim path As String = context.Server.MapPath(request.QueryString("file"))
      Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
      If file.Exists And EnabledTypes.Contains(file.Extension.ToLower()) Then
        context.Response.Clear()
        context.Response.AddHeader("Content-Disposition", _
            "attachment; filename=" & file.Name)
        context.Response.AddHeader("Content-Length", file.Length.ToString())
        context.Response.ContentType = "application/octet-stream"
        context.Response.WriteFile(file.FullName) 
      Else
        context.Response.ContentType = "plain/text"
        context.Response.Write("This file does not exist.") 
      End If
    Else
      context.Response.Write("Please provide a file to download.")
    End If
  End Sub

  Public ReadOnly Property IsReusable() As Boolean _
        Implements IHttpHandler.IsReusable
    Get
        Return True
    End Get
  End Property
End Class

Make sure you implement a check for image files, otherwise you have a potential security problem. (Users could download web.config where database passwords are stored)

The link would become:

<a href="/DownloadImage.ashx?file=/images/logo.gif">Download image</a>

You should clear the header and provide the correct header and content type

Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=somefilename")
Response.ContentType = "image/jpeg"
Response.TransmitFile(Server.MapPath("/xyz.jpg"));
Response.End();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top