<requestFiltering>
<hiddenSegments>
<add segment="Test"/>
</hiddenSegments>
</requestFiltering>
我在 web.config 中有帶有上述代碼的網站(ASP.Net 網路表單),以防止直接訪問 Test 檔案夾檔案,有時我想從 Test 檔案夾訪問我網站內的影像,有什么方法可以排除此功能以進行讀取檔案。
例如
當用戶訪問網站時,我想將與他相關的影像顯示為縮略圖,當我將影像源添加到測驗檔案夾內的物理檔案時,網站無法讀取檔案。
注意:我可以毫無問題地將測驗檔案夾中的檔案下載給用戶。
有沒有辦法在不下載整個檔案的情況下讀取檔案內容?
謝謝
uj5u.com熱心網友回復:
您可以使用隱藏的代碼來顯示有問題的檔案。
請記住:代碼隱藏:任何檔案參考都是一個完整的平面 jane 檔案路徑名。
WEB:控制元件和 Web URL:它們由您的 Web 配置安全控制。
所以,假設我們有一個包含一些圖片的檔案夾。我們使用網路配置保護該檔案夾。現在所有用戶都不能使用、查看、輸入該 URL。但是,您仍然可以使用后面的代碼,并且如前所述,它們是飛機 jane 完整的 Windows 路徑名。因此,請記住隱藏代碼與網頁和使用 web.config 安全性之間的“主要”區別。
因此,您可以說有一個 gridview,并在該網格中顯示影像,但圖片的 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
我們現在有這個:

So remember that code behind can go read files directly from those folders - it quite much "ignores" the web config and security you apply against a web url or folder when you use code behind. Thus, for say PDF's, or images you want to display, but not allow direct URL's to the folder? Use code behind - all code behind uses full simple plane jane windows path names. You can convert the path name from the folder (and url) with Server.MapPath("some web url goes here").
Once you convert to that internal path name - then your code behind is free to get/grab/use that file - it quite much ignores the web config settings.
So, in above, i had/have a column in the database with path names to the images - but I don't have to expose that column into the GridView, and users can't see, or type in valid URL's to that folder with pictures - since I prevented them from doing so with the web.config settings for that folder.
Edit: Downloading the file when user clicks on the image
So, what about allowing the user to download the file when you click on the image button?
Well, we just need to wire up a simple click event for that button.
We can't double click on the button to get that code behind click event, (since it inside of the grid). But you can start typing in the markup like this:

Choose create new event - it LOOKS like nothing occurs, but if we flip to code behind, you see the image button click event was created.
當用戶單擊該按鈕時,我們將下載檔案。
該代碼可能如下所示:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395534.html
上一篇:在vb中顯示最貴產品的名稱
下一篇:在FlowPanel內克隆面板
