Dim fName1 As String = e.Item.Cells(1).Text.Trim '文件名
Dim fName As String
'****************文件下载***********************************************
Try
fName = viewstate("curDirectory") + fName1 ‘全路径文件名
Dim fInfo As System.IO.FileInfo = New FileInfo(fName)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fName1, System.Text.Encoding.UTF8))
Response.AddHeader("Content-Length", fInfo.Length.ToString())
Response.ContentType = "application/octet-stream"
Dim MyFileStream As FileStream = New FileStream(fName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
Dim FileSize As Long = MyFileStream.Length
Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, Int32.Parse(FileSize))
MyFileStream.Close()
Response.BinaryWrite(Buffer)
Catch ex As Exception
Page.RegisterStartupScript("look", "<script language='javascript'>window.alert('文件不存在!' )</script>")
Exit Sub
Finally
Response.End()
End Try
Dim fName As String
'****************文件下载***********************************************
Try
fName = viewstate("curDirectory") + fName1 ‘全路径文件名
Dim fInfo As System.IO.FileInfo = New FileInfo(fName)
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.Buffer = True
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fName1, System.Text.Encoding.UTF8))
Response.AddHeader("Content-Length", fInfo.Length.ToString())
Response.ContentType = "application/octet-stream"
Dim MyFileStream As FileStream = New FileStream(fName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
Dim FileSize As Long = MyFileStream.Length
Dim Buffer(CInt(FileSize)) As Byte
MyFileStream.Read(Buffer, 0, Int32.Parse(FileSize))
MyFileStream.Close()
Response.BinaryWrite(Buffer)
Catch ex As Exception
Page.RegisterStartupScript("look", "<script language='javascript'>window.alert('文件不存在!' )</script>")
Exit Sub
Finally
Response.End()
End Try
解决方案 »
- JS问题求解
- 调用函数奇怪。。。System.NullReferenceException: 未将对象引用设置到对象的实例
- 大家有没有遇到过这要的问题: 无法在Web服务器上启动调试,未将项目配置为进行调试.
- 如何能在.cs文件中动态更改<%@ Register>的Src属性?
- 怎样在用户控件内用WEB控件
- 请大家来讨论ASP.NET中生成Excel文件的各种方法
- 关于网页重定向的问题!
- 请问各位ASP。NET高手,做页面时用什么工具,我以前用dreamweaver
- 在 asp.net 中 ,用C# /VB.NET 怎样在建立虚拟目录后指定一个首页(如 :myindex.asp作为首页)! ??
- 未知的服务器标记“ASP:”。
- 急问:在客户端,怎样能够得到DataGrid中隐藏列的值
- 这个字符串怎么写?
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Net" %>
<script runat="server">
Sub Button1_Click(s As Object,e As EventArgs)
DownloadFile("c:\rt.gif",true)
End SubSub DownloadFile(ByVal fname As String, ByVal forceDownload As Boolean)
Dim path As Path
Dim fullpath = path.GetFullPath(fname)
Dim name = path.GetFileName(fullpath)
Dim ext = path.GetExtension(fullpath)
Dim type As String = ""
If Not IsDBNull(ext) Then
ext = LCase(ext)
End If
Select Case ext
Case ".htm", ".html"
type = "text/HTML"
Case ".txt"
type = "text/plain"
Case ".doc", ".rtf"
type = "Application/msword"
Case ".csv", ".xls"
type = "Application/x-msexcel"
Case Else
type = "text/plain"
End Select
If (forceDownload) Then
Response.AppendHeader("content-disposition", _
"attachment; filename=" + name)
End If
If type <> "" Then
Response.ContentType = type
End If
Response.WriteFile(fullpath)
Response.End()
End Sub
</script>
<form runat="server">
<asp:Button id="button1" onclick="Button1_Click" runat="server" Text="DownLoadFile"/>
</form>