2010-04-07 22 views
2

我有一個GridView,它顯示了以前上傳到服務器的文件列表,並使用HyperLink控件可以下載它,每當用戶單擊提供的鏈接之一時,我需要強制下載,以便文件不直接在瀏覽器上打開(它們通常是圖像)。目前我有一個側面服務器功能,強制下載,但我不知道如何分配這個功能,每個鏈接時,用戶點擊它。如何強制下載鏈接在GridView內的文件?

+0

你是如何 「強迫」 下載?一個代碼示例可能會有所幫助 – Glennular 2010-04-07 19:48:13

回答

0

創建一個下載頁面在GridView通過GET查詢

需要下載參數創建一個模板字段與超級鏈接列在對外窗口打開下載頁面下載發送的文件後,它關閉由它的自我,我提供了一個示例代碼

<Columns> 
<asp:TemplateField> 
    <ItemTemplate> 
    <a href="downloadPage.aspx?filename=<%# Eval("File") %>" target="_blank">Download</a> 
    </ItemTemplate> 
</asp:TemplateField> 
</Columns> 
1

謝謝你們,我的工作了使用的ImageButton並賦予它調用我的ForceDownload方法的RowCommand:

HTML:

<asp:TemplateField HeaderText="Download File"> 
    <ItemTemplate> 
     <asp:ImageButton ID="ibDownload" runat="server" CommandName="Download" CommandArgument='<%# Bind("path") %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

代碼背後:

Private Sub gvFileList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFileList.RowCommand 
    If e.CommandName = "Download" Then 
     Dim myPath = Server.MapPath(e.CommandArgument) 
     ForceDownload(HttpContext.Current, myPath) 
    End If 
End Sub