1 ''' <summary>
2 ''' 下载文件
3 ''' </summary>
4 ''' <param name="filename">文件物理地址</param>
5 Protected Sub DownloadFile()Sub DownloadFile(ByVal filename As String)
6
7 Dim saveFileName As String = "test.xls"
8 Dim intStart As Integer = filename.LastIndexOf("//")+1
9 saveFileName = filename.Substring(intStart,filename.Length-intStart)
10
11 Dim fi As System.IO.FileInfo = New System.IO.FileInfo(filename)
12 Dim fileextname As String = fi.Extension
13 Dim DEFAULT_CONTENT_TYPE As String = "application/unknown"
14 Dim regkey As RegisTryKey,fileextkey As RegisTryKey
15 Dim filecontenttype As String
16 Try
17 regkey=RegisTry.ClassesRoot
18 fileextkey=regkey.OpenSubKey(fileextname)
19 filecontenttype=fileextkey.GetValue("Content Type",DEFAULT_CONTENT_TYPE).ToString()
20 Catch
21 filecontenttype=DEFAULT_CONTENT_TYPE
22 End Try
23
24
25 Response.Clear()
26 Response.Charset = "utf-8"
27 Response.Buffer= True
28 Me.EnableViewState = False
29 Response.ContentEncoding = System.Text.Encoding.UTF8
30
31 Response.AppendHeader("Content-Disposition","attachment;filename=" + saveFileName)
32 Response.ContentType=filecontenttype
33
34 Response.WriteFile(filename)
35 Response.Flush()
36 Response.Close()
37
38 Response.End()
39 End Sub
Asp.net直接保存文件到客户端
通常的文件下载是用跳出窗口实现的,但是有个问题,就是会被广告拦截软件直接拦截掉,另我非常的头痛,于是寻找更好的解决方法.看了用Response.BinaryWrite写文件流一文之后觉得确实可以如此。 如下代码实现了此功能,解决了文件格式问题(就是只将流输出,但无法正确识别文件格式),并且从注册表读取文件的ContentType 。