可以在超链接处,链向此页,传递文件地址(filename),在load事件里面用到下面代码
Try
'在此处放置初始化页的用户代码
Dim strFileName As String = Request.QueryString("FileName")
Dim toDownload As System.IO.FileInfo = New System.IO.FileInfo(Server.UrlDecode(strFileName))
Const ChunkSize As Long = 10000'一次取多少字节
Dim buffer(ChunkSize) As Byte
Response.Clear()
Dim iStream As System.IO.FileStream = System.IO.File.OpenRead(strFileName)
Dim dataLengthToRead As Long = iStream.Length
Dim tempfileName As String
tempfileName = HttpUtility.UrlEncode(toDownload.Name)'改变中文文件名乱码问题
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=" & tempfileName)
While dataLengthToRead > 0 AndAlso Response.IsClientConnected
Dim lengthRead As Integer = _
iStream.Read(buffer, 0, ChunkSize)'读取
Response.OutputStream.Write(buffer, 0, lengthRead)'写到头里面
Response.Flush()
dataLengthToRead = dataLengthToRead - lengthRead
End While
Response.Close()
Catch ex As Exception
Response.Close()
End Try
本文介绍了一种在ASP.NET中实现文件下载的方法,通过解析请求中的文件名参数,读取指定路径下的文件并将其发送给客户端进行下载。文中特别关注了如何处理中文文件名的乱码问题。
2191

被折叠的 条评论
为什么被折叠?



