ASP.NET-如何仅对某些文件类型执行此功能?

ASP.NET-如何仅对某些文件类型执行此功能?,asp.net,download,file-extension,Asp.net,Download,File Extension,只希望将此函数与特定的文件扩展名(我定义的)一起使用。如何在ASP.net中执行此操作 基本上,下面的代码允许下载任何文件,只需将文件指向此页面.aspx?file={path to file+file}我想做的是,如果文件没有需要定义的数组的扩展名,则退出此页面。例如:只希望以下扩展能够通过以下代码下载:.jpg、.jpeg、.bmp、.png、.gif、.pdf、.doc、.docx <%@ Page language="vb" runat="server" explicit="tru

只希望将此函数与特定的文件扩展名(我定义的)一起使用。如何在ASP.net中执行此操作

基本上,下面的代码允许下载任何文件,只需将文件指向此页面
.aspx?file={path to file+file}
我想做的是,如果文件没有需要定义的数组的扩展名,则退出此页面。例如:只希望以下扩展能够通过以下代码下载:.jpg、.jpeg、.bmp、.png、.gif、.pdf、.doc、.docx

<%@ 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 <> "" 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("Please provide a file to download.")
    End If
End Sub
</script>

子页面加载(发送方作为对象,E作为事件参数)
Dim strRequest As String=Request.QueryString(“文件”)
如果请求为“”,则
Dim路径为字符串=Server.MapPath(strequest)
Dim文件为System.IO.FileInfo=新System.IO.FileInfo(路径)
如果文件
答复:Clear()
Response.AddHeader(“内容处置”、“附件;文件名=“&file.Name”)
Response.AddHeader(“Content Length”,file.Length.ToString())
Response.ContentType=“应用程序/八位字节流”
Response.WriteFile(file.FullName)
答复.完
其他的
Write(“此文件不存在。”)
如果结束
其他的
写(“请提供一个要下载的文件。”)
如果结束
端接头
如何更改此项以将此函数排除在所有文件扩展名上,但列出的文件扩展名除外:.jpg、.jpeg、.bmp、.png、.gif、.pdf、.doc、.docx

<%@ 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 <> "" 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("Please provide a file to download.")
    End If
End Sub
</script>

我在想,如果file.Exists,则需要将其更改为:
如果file.Exists和file.Extension…

正如我在评论中提到的,您可以根据请求捕获它。对于多重比较,有一个更优雅的解决方案,但为了简单起见,这是可行的:

Dim strRequest As String = Request.QueryString("file").ToLower

If strRequest <> "" AndAlso (strRequest.EndsWith(".jpg") OrElse strRequest.EndsWith(".jpeg") OrElse strRequest.EndsWith(".png") OrElse strRequest.EndsWith(".gif") OrElse strRequest.EndsWith(".pdf") Or strRequest.EndsWith(".doc") Or strRequest.EndsWith(".docx") Or strRequest.EndsWith(".bmp")) Then
   '....
End If
Dim strequest As String=Request.QueryString(“文件”).ToLower
如果strequest“”和lso(strequest.EndsWith(“.jpg”)或lse strequest.EndsWith(“.jpeg”)或lse strequest.EndsWith(“.png”)或lse strequest.EndsWith(.gif”)或lse strequest.EndsWith(“.docx”)或strequest.EndsWith(“.docx”)或strequest.EndsWith(“.bmp”),则
'....
如果结束

为什么你不能按要求抓到它?例如,
如果strRequest“”和strrrequest.EndsWith(“.jpg”),那么
是的,这也可以。。。把你的答案贴出来。谢谢但是我需要有多个
结尾。此外,它还应该阻止大写扩展。@YuriyGalanter-我现在可以使用它,使用
EndsWith
,它也可以用于大写扩展。万分感谢!如果你想,你可以发布你的答案,我会把它标记为答案,但无论如何,我已经用你的评论解决了这个问题!我不知道如何使用
EndsWith
,所以这帮了大忙!谢谢,将此标记为我的答案,但我使用了以下选项:
如果strRequest“”和strRequest.EndsWith(“.jpg”)或strRequest.EndsWith(“.jpeg”)或strRequest.EndsWith(“.png”)或strRequest.EndsWith(“.gif”)或strRequest.EndsWith(“.doc”)或strRequest.EndsWith(“.docx”)或strRequest.EndsWith(“.bmp”)然后
这似乎完全符合我的预期。我能问一下为什么我们需要
而不仅仅是
?如果我们使用
,我还能在这里做我所有的
语句吗?基本上,
是如何改变if语句的,而不是仅仅使用
?很高兴它对您有用。关于
AndAlso
-这是短路版本的
。如果您使用
-VB.NET评估所有部分的条件,即使在首先检查之后-其余部分没有意义。在您的情况下,如果使用
,并且strequest为空,则它仍将继续检查扩展,即使它没有用处。如果您使用
,它将立即停止。
之间存在相同的差异。所以记住这一点,并记住操作符优先级(您需要使用parentesses),我将用一个代码片段更新我的答案。