Asp.net 读取PDF文件到页面

本文介绍了一种在ASP.NET应用程序中处理PDF文件的方法,包括如何读取PDF文件并将其发送到客户端浏览器的过程。具体步骤涉及检查文件是否存在、打开文件流、读取文件内容并设置HTTP响应头等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        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阅读器,否者是读不出来的

转载于:https://www.cnblogs.com/lwl0606/archive/2008/11/25/1340764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值