asp.net网页中上传并且浏览pdf文件的实现
本文主要讲解在asp.net中的gridview中浏览pdf文件。下面来看一下具体的实现:
第一步,使用sqlserver 创建一个数据库表。

第二步,新建一个webform,命名为uploadpdf.aspx。

第三步,在该页面中添加一个upload控件,两个button控件,代码如下。
<asp:fileupload ID="Fileupload1" runat="server"></asp:fileupload>
<asp:Button ID="Btnupload" runat="server" Text="上传" onclick="Btnupload_Click" />
<asp:Button ID="Btncancel" runat="server" Text="取消" />
<asp:Label ID="alert" runat="server" />

第四步,单击上传按钮在Btnupload_Click事件中写入上传代码。
try
{
byte[] pdf = null;
if (Fileupload1.HasFile & Fileupload1.PostedFile != null)//判断上传文件是否为空
{
HttpPostedFile file = Fileupload1.PostedFile;
pdf = new byte[file.ContentLength];//创建一个文件长度的字节数组
file.InputStream.Read(pdf, , file.ContentLength);//把文件写入二进制字节数组pdf中 } string connectionStr = System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionStr);
con.Open();
string sql = "insert into tbl_pdf (pdfFile,FileName) values(@pdfFile,@FileName)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@pdfFile", pdf);
cmd.Parameters.AddWithValue("@FileName", Fileupload1.PostedFile.FileName);
cmd.ExecuteNonQuery();
alert.Text = "file uploaded successfully";
con.Close(); }
catch (Exception ex)
{
Response.Write(ex.Message);
}
到这里,可以上传pdf文件保存到数据库中。
第五步,在uploadpdf.aspx添加一个gridview控件和一个数据源控件。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="pdfview">
<Columns>
<asp:BoundField DataField="Doc_ID" HeaderText="Doc_ID" InsertVisible="False"
ReadOnly="True" SortExpression="Doc_ID" />
<asp:BoundField DataField="FileName" HeaderText="FileName"
SortExpression="FileName" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="VIEW" CommandArgument='<%# Eval("Doc_ID") %>'></asp:LinkButton> </ItemTemplate>
</asp:TemplateField> </Columns>
</asp:GridView>
<asp:SqlDataSource ID="pdfview" runat="server"
ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT * FROM [tbl_pdf]"></asp:SqlDataSource>
第六步,新建一个处理程序来读取pdf文件。

第七步,在新建的处理程序下面添加处理代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration; namespace test
{
/// <summary>
/// Pdfhandler 的摘要说明
/// </summary>
public class Pdfhandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
Int32 theID;
if (context.Request.QueryString["id"] != null)
theID = Convert.ToInt32(context.Request.QueryString["id"]);
else
throw new ArgumentException("no parameter specified");
context.Response.ContentType = "Application/pdf";
Stream strm = DisplayImage(theID) ;
byte[] buffer = new byte[];
int byteseq = strm.Read(buffer,,);
while (byteseq > )
{
context.Response.OutputStream.Write(buffer, , byteseq);
byteseq = strm.Read(buffer, , );
} }
public Stream DisplayImage(int theID)
{
string str = System.Configuration.ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(str);
string sql = "SELECT pdfFile FROM [tbl_pdf] where Doc_ID = @Doc_ID ";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.Parameters.AddWithValue("Doc_ID",theID);
con.Open();
object theImg = cmd.ExecuteScalar();
try
{
return new MemoryStream((byte[])theImg);
}
catch
{ return null;
}
finally
{
con.Close();
}
}
第八步,这时应该在gridview中添加一个linkbutton点击连接查看。
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkView" runat="server" Text="View" OnClick="VIEW" CommandArgument='<%# Eval("Doc_ID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
第九步,在uploadpdf.aspx.cs下面新建一个点击链接的方法。
public void VIEW(object sender, EventArgs e)
{
int id = int.Parse((sender as LinkButton).CommandArgument);
Response.Redirect("Pdfhandler.ashx?Id="+id+""); }
到这里就大功告成,然后进行测试。
测试结果如下

点击view连接,这时结果如下。

asp.net网页中上传并且浏览pdf文件的实现的更多相关文章
- Ajax在ASP.NET MVC中上传
HomeController.cs using System; using System.Collections.Generic; using System.Linq; using System.We ...
- 在线打开,浏览PDF文件的各种方式及各种pdf插件------(MS OneDrive/google drive & google doc/ github ?raw=true)
在线打开,浏览PDF文件的各种方式: 1 Google drive&doc (国内不好使,you know GFW=Great Firewall) 1. google drive: 直接分 ...
- https://github.com/Lushenggang/show-pdf在线浏览pdf文件在线浏览pdf文件
在线浏览pdf文件 https://github.com/Lushenggang/show-pdf https://github.com/Lushenggang/show-pdf
- web中浏览PDF文件
1.在web中浏览pdf文件. 2.支持大多数主流浏览器,包括IE8 3.参考网址: https://pdfobject.com/ http://mozilla.github.io/pdf.js/ & ...
- WPF 浏览PDF 文件
添加成功后会在工具箱里看到下图所示的控件.打开VS2010,新建项目(WpfPDFReader),右键项目添加User Control(用户控件).因为Adobe PDF Reader COM 组件是 ...
- 在线浏览pdf文件,pdfobject的简单使用
该js插件,官网有详细的使用教程(网址:http://www.pdfobject.com/examples/).打开里面的例子后,查看新打开页面,打开并查看该页面的源代码. 需要的材料: 1.PDFo ...
- ASP.NET MVC 项目直接预览PDF文件
背景及需求 项目使用的是MVC4框架,其中有一个功能是根据设置生成PDF文件,并在点击时直接预览. 实现过程 1.第一版实现代码: HTML内容 @{ Layout = null; } <!DO ...
- 怎么用ABBYY在线浏览PDF文件
ABBYY FineReader 让您可以从在线存储服务中打开图像或 PDF 文件,并将已识别文本保存至在线存储服务中,如 Dropbox.SkyDrive 或 Google Drive 等.通过在 ...
- 在asp.net mvc中上传大文件
在asp.net mvc 页面里上传大文件到服务器端,需要如下步骤: 1. 在Control类里添加get 和 post 方法 // get method public ActionResult Up ...
随机推荐
- Codeforces Round #105 (Div. 2) ABCDE
A. Insomnia cure 哎 只能说英语太差,一眼题我看了三分钟. 题意:给5个数k, l, m, n 和 d,求1~d中能被k, l, m, n 至少一个整除的数的个数. 题解:…… 代码: ...
- EDM
今天,夏令营需要发推广EDM. 之前的edm都是文字版, 今天摸索了一下,简单写了些码, 效果还不错. 演示:http://baike.baidu.com/cms/s/bkcampus/summerc ...
- Java序列化之transient和serialVersionUID的使用
package FileDemo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IO ...
- fscanf(格式化字符串输入)
fscanf(格式化字符串输入) 相关函数 scanf,sscanf 表头文件 #include<stdio.h> 定义函数 int fscanf(FILE * stream ,const ...
- python 使用模块
Python本身就内置了很多非常有用的模块,只要安装完毕,这些模块就可以立刻使用. 我们以内建的sys模块为例,编写一个hello的模块: #!/usr/bin/env python # -*- co ...
- uCos 没有延时Tick滴答定时器测试
原来学uCos只是表面,今天才发现uCos没有心跳也是可以活的,只是延时功能. 即:OSTimeDly.OSTimexxx 头的功能不能使用. 如果有是用OSTimexxx,任务将会卡死.其实,OST ...
- session 重写进入redis测试
在实际业务中,当session存储过多 或者操作频繁,业务逐渐扩展的时候,文件存储已经无法满足session操作速度和需求,可以考虑用数据库或者nosql的redis来存储session,本文讲解如何 ...
- Stage3D学习笔记(四):正交矩阵
我们上一章节显示图片的时候,会发现我们制定的顶点在Stage3D中其实是存在一个区间的: x轴(从左到右):[-1.0-1.0] y轴(从下到上):[-1.0-1.0] z轴(从近到远):[0-1.0 ...
- Modernizr 与 Polyfill
之前提到,Modernizr 是 HTML5 和 CSS3 的特性检测工具,这里简单介绍一下它的用法.最简单的用法是在页面的 <head> 中添加 Modernizr 的 JavaScri ...
- 教你50招提升ASP.NET性能(十二):在生产环境,仔细考虑你需要记录哪些日志
(18)When in production, carefully consider what you need to log 招数18: 在生产环境,仔细考虑你需要记录哪些日志 Many peopl ...