我在这个网站上没有apache支持,但我需要能够允许图像只能在某个目录中下载。我怎样才能做到这一点?这个站点只有ASP.NET支持,它杀了我!注意到此链接:How to download files in mvc3?但不确定将代码放在何处,或者即使该代码可以帮助我。
任何帮助将不胜感激!一个起点或什么......
有没有办法可以在HTML中执行此操作?比如,例如,将下载链接设置为指向html文件,HTML文件在其中抓取图像文件并使其可以自行下载?
到目前为止,我在名为default.asp
的文件中有以下ASP代码开始下载就好了,但它下载了一个空文件(download.jpg)。如何将以下代码指向要下载的实际图像文件?
<%@ Language=VBScript %>
<% Option Explicit
Response.ContentType = "application/octet-stream"
Response.AddHeader "Content-Disposition", "attachment; filename=" + "download.jpg"
%>
即使在同一目录下,我也有一个名为“download.jpg”的文件,但它从不下载实际图像。它会下载一个90字节的空图像文件。
我甚至没有运气就试过这个:
<%@ 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
%>
是的,我在服务器根目录下的images / 07awardee.png中有07awardee.png文件,甚至在default.asp所在的文件夹根目录中。 Arggg!什么给这里?该文件现在有点大,为392字节,但它仍然不能作为图像文件读取...我一直在搜索互联网,这应该工作,但不是!这可能是什么问题?
答案 0 :(得分:2)
带有Request.End的.aspx页面会抛出一个ThreadAbortException,这对服务器性能有害(太多这些甚至会导致服务器崩溃)。所以你要避免这种情况。 http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx
我处理此问题的方法是使用HttpHandler(.ashx)并使用它来提供可下载的图像文件。我使用了一些代码,因为我的实现是在C#中,并且有更多的代码(包括图像缩放选项等):
//<%@ 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
确保对图像文件执行检查,否则您可能会遇到安全问题。 (用户可以下载存储数据库密码的web.config)
链接将变为:
<a href="/DownloadImage.ashx?file=/images/logo.gif">Download image</a>
答案 1 :(得分:1)
您应该清除标题并提供正确的标题和内容类型
Response.Clear()
Response.AppendHeader("Content-Disposition", "attachment; filename=somefilename")
Response.ContentType = "image/jpeg"
Response.TransmitFile(Server.MapPath("/xyz.jpg"));
Response.End();
答案 2 :(得分:1)
<%@ 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>
现在,当您想要下载文件(任何文件)时,只需将其链接如下:
<a href="download.aspx?file=/images/logo.gif">Download the logo image</a>
这就是她写的全部内容!