从 hiddenSegments 文件夹中读取文件 (ASP.Net)

Read files from hiddenSegments folder (ASP.Net)

            <requestFiltering>
            <hiddenSegments>
                <add segment="Test"/>
            </hiddenSegments>
        </requestFiltering>

我有一个网站(ASP.Net 网络表单),上面的代码在 web.config 中,以防止直接访问测试文件夹文件,有时我想从测试文件夹访问我网站内的图像是有什么方法可以排除此功能以读取文件。

例如

当用户访问网站时,我想将与他相关的图像显示为缩略图,当我将图像源添加到测试文件夹中的物理文件时,网站无法读取文件。

注意:我可以毫无问题地将测试文件夹中的文件下载给用户。

有什么方法可以在不下载整个文件的情况下读取文件内容吗?

谢谢

您可以使用隐藏代码来显示有问题的文件。

记住: 代码隐藏:任何文件引用都是一个完整的平面简文件路径名。

WEB:控件和 Web URL:它们由您的 Web 配置安全性控制。

假设我们有一个包含一些图片的文件夹。我们使用 Web 配置保护该文件夹。现在所有用户都不能使用,看到,输入那个URL。但是,您仍然可以使用隐藏代码,并且如前所述,它们是平面简完整 windows 路径名。因此,请记住代码隐藏与网页代码之间的“主要”区别以及使用 web.config 安全性。

因此,您可以说有一个网格视图,并在该网格中显示图像,但是图片的 URL 无效 - 我们简单地使用代码隐藏来读取图片文件和“将其流式传输到浏览器。

此设置(比如网格)将如下所示:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" CssClass="table"  width="65%" >
        <Columns>
            <asp:BoundField DataField="Fighter" HeaderText="Fighter" ItemStyle-Width="160px"  />
            <asp:BoundField DataField="Engine" HeaderText="Engine"  />
            <asp:BoundField DataField="Thrust" HeaderText="Thrust (lbs)"  />
            <asp:BoundField DataField="Description" HeaderText="Description"  />

            <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <asp:ImageButton ID="cmdImage" runat="server" Height="78px" Width="132px" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

加载网格的代码如下所示:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadGrid()
    End If
End Sub

Sub LoadGrid()

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand("SELECT * FROM Fighters ORDER BY Fighter", conn)
            conn.Open()
            Dim rstData As New DataTable
            rstData.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rstData
            GridView1.DataBind()
        End Using
    End Using

End Sub

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound


    If e.Row.RowType = DataControlRowType.DataRow Then

        ' get data row for grid
        Dim gData As DataRowView = e.Row.DataItem

        Dim ImageURL As String =
            Server.MapPath(gData("ImagePath"))    ' get raw windows path name

        Dim cmdImage As ImageButton = e.Row.FindControl("cmdImage")
        Dim iBytes() As Byte
        iBytes = File.ReadAllBytes(ImageURL)
        cmdImage.ImageUrl = "Data:Image/jpg;base64," & Convert.ToBase64String(iBytes)

    End If

End Sub

我们现在有这个:

因此请记住,代码隐藏可以直接从这些文件夹中读取文件 - 当您使用代码隐藏时,它会非常“忽略”您针对 Web url 或文件夹应用的 Web 配置和安全性。因此,对于要显示的 PDF 或图像,但不允许直接 URL 到文件夹?使用隐藏代码 - 所有隐藏代码都使用完整的简单平面 jane windows 路径名。您可以使用 Server.MapPath("some web url goes here").

从文件夹(和 url)转换路径名

一旦您转换为该内部路径名 - 然后您的代码可以自由 get/grab/use 该文件 - 它几乎忽略了网络配置设置。

所以,在上面,我 had/have 数据库中的一个列,其中包含图像的路径名 - 但我不必将该列公开到 GridView 中,用户看不到,或者在带有图片的文件夹中输入有效的 URL - 因为我使用该文件夹的 web.config 设置阻止了他们这样做。

编辑:当用户点击图片时下载文件

那么,当您单击图像按钮时允许用户下载文件怎么样?

好吧,我们只需要为该按钮连接一个简单的点击事件。

我们无法双击按钮来获取点击事件背后的代码(因为它在网格内部)。但是您可以像这样开始输入标记:

选择创建新事件 - 它看起来没有任何反应,但如果我们转到代码后面,您会看到图像按钮点击事件已创建。

当用户单击该按钮时,我们将下载文件。

该代码可能如下所示:

Protected Sub cmdImage_Click(sender As Object, e As ImageClickEventArgs)

    Dim cmdImage As ImageButton = sender
    Dim gRow As GridViewRow = cmdImage.NamingContainer

    Dim PkRowID As Integer = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    Dim strSQL As String =
        "SELECT ID,ImagePath FROM Fighters WHERE ID = " & PkRowID

    Dim strPicPath As String = ""

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            Dim rstData As New DataTable
            rstData.Load(cmdSQL.ExecuteReader)
            strPicPath = rstData.Rows(0).Item("ImagePath")
            strPicPath = Server.MapPath(strPicPath)
        End Using
    End Using

    Dim strPicFile = Path.GetFileName(strPicPath)
    Dim iBytes() As Byte = File.ReadAllBytes(strPicPath)
    Response.ClearHeaders()
    Response.AddHeader("content-disposition", "attachment;filename=" & strPicFile)
    Response.BinaryWrite(iBytes)
    Response.Flush()
    Response.End()

End Sub