從網站跳出詢問視窗是否下載檔案(Content-Disposition,attachment; filename,Response.ContentType,WriteFile,HttpContext.Current.Response,flush,close,end)

  • 21152
  • 0
  • 2012-06-13

摘要:從網站跳出詢問視窗是否下載檔案


 Response.ContentType = "application/x-zip-compressed"
            Response.AddHeader("Content-Disposition", "attachment; filename=""" & HttpUtility.UrlEncode("testZip.zip", System.Text.Encoding.Default) & """")
            Response.Clear()
            Response.WriteFile(strFileDirPath & "\testZip.zip")
            Response.Flush()
            Response.Close()
            HttpContext.Current.ApplicationInstance.CompleteRequest()

當然這個test.zip檔案事先已經先偷偷的儲存在電腦裡囉~~

一般在這段程式碼之前都會寫一些寫入檔案的程式碼拉~然後會把test.zip藏在一個很難發現的地方

還有補充一下

1.Response.ContentType的種類多如牛毛,看你要給人家下載的檔案是甚麼種類,再去google搜尋你要的種類吧

2.如果將Response封裝在.vb類別檔案而非aspx.vb的時候,必須要在HttpContext.Current.Response底下response,如下


 With HttpContext.Current.Response
            .ContentType = "application/zip"
            .AddHeader("Content-Disposition", "attachment; filename=""" & HttpUtility.UrlEncode(strZipFileName, System.Text.Encoding.Default) & """")
            .Clear()
            .WriteFile(strFileDir & "\" & strZipFileName)
            .Flush()
            .Close()
            
        End With
HttpContext.Current.ApplicationInstance.CompleteRequest()