[原]Asp.net中以对话框形式下载文件(VB.net)
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
Dim imgUrl As String = Me.Request.QueryString("url") '//取出文件的物理路径。
Me.DownloadFile(imgUrl)
End Sub
Private Function DownloadFile(ByVal filename As String)
Dim saveFileName As String
Dim startIndex As Int32 = filename.LastIndexOf("\") + 1
saveFileName = filename.Substring(startIndex)
Dim fi As System.IO.FileInfo = New System.IO.FileInfo(filename)
Dim fileExtName As String = fi.Extension
Dim DEFAULT_CONTENT_TYPE As String = "application/unknown"
Dim regKey, fileExtKey As Microsoft.Win32.RegistryKey
Dim fileContentType As String
Try
regKey = Microsoft.Win32.Registry.ClassesRoot
fileExtKey = regKey.OpenSubKey(fileExtName)
fileContentType = fileExtKey.GetValue("Content Type", DEFAULT_CONTENT_TYPE)
Catch ex As Exception
fileContentType = DEFAULT_CONTENT_TYPE
End Try
Me.Response.Clear()
Response.Charset = "utf-8"
Me.Response.Buffer = True
Me.EnableViewState = False
Me.Response.ContentEncoding = System.Text.Encoding.UTF8
Me.Response.AppendHeader("Content-Disposition", "attachment;filename=" & saveFileName)
Me.Response.ContentType = fileContentType
Me.Response.WriteFile(filename)
Me.Response.Flush()
Me.Response.End()
End Function
我的这个程序能够下载Jpg图片等文件,但在保存对话框中的文件名,如果是汉字就不能正常显示(显示乱码),字母和数字都能正常显示,希望各位能帮忙解决一下。先谢了