现在的位置: 首页 > 综合 > 正文

Asp.net 读取PDF文件到页面

2012年01月06日 ⁄ 综合 ⁄ 共 826字 ⁄ 字号 评论关闭
        Try
            
Dim pdf As String = "./Files/ty1.pdf"
            pdf 
= Server.MapPath(pdf)

            'read all bytes
            If File.Exists(pdf) = False Then
                
Throw New Exception("This report is not created")
            
End If
            
Dim fs As FileStream = File.Open(pdf, FileMode.Open)
            
Dim buffer() As Byte = New Byte(fs.Length) {}
            fs.Read(buffer, 
0, buffer.Length)
            fs.Close()

            'write to response
            Response.Clear()
            Response.ContentType 
= "application/pdf"
            Response.AddHeader(
"content-disposition""filename=ty1.pdf")
            Response.AddHeader(
"content-length", buffer.Length.ToString())
            Response.BinaryWrite(buffer)
            Response.End()
        
Catch ex As Exception

        End Try

 

一定要装PDF阅读器,否者是读不出来的

抱歉!评论已关闭.