2016年2月17日 星期三

強制下載檔案

強制下載檔案    參考
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim index As Integer = CType(CType(sender, LinkButton).Parent.Parent, GridViewRow).RowIndex
        Dim lb_url As Label = CType(GridView1.Rows(index).FindControl("lb_url"), Label)


        Dim p_url As String = Server.MapPath(lb_url.Text)

        Dim wc As New System.Net.WebClient()
        Dim a As Byte() = wc.DownloadData(p_url)
        Dim FileName As String = System.IO.Path.GetFileName(p_url)


        Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", FileName))
        Response.BinaryWrite(a)
 

        Response.End() 
End Sub

IE下載中文檔名出現亂碼  參考
在下載的名稱加上HttpUtility.UrlPathEncode
ex.
Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", HttpUtility.UrlPathEncode(FileName)))

沒有留言:

張貼留言