Asp.net 读取PDF文件到页面
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
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阅读器,否者是读不出来的