本文主要讲解在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文件的实现的更多相关文章

  1. Ajax在ASP.NET MVC中上传

    HomeController.cs using System; using System.Collections.Generic; using System.Linq; using System.We ...

  2. 在线打开,浏览PDF文件的各种方式及各种pdf插件------(MS OneDrive/google drive & google doc/ github ?raw=true)

    在线打开,浏览PDF文件的各种方式: 1 Google drive&doc   (国内不好使,you know GFW=Great Firewall) 1. google drive: 直接分 ...

  3. https://github.com/Lushenggang/show-pdf在线浏览pdf文件在线浏览pdf文件

    在线浏览pdf文件 https://github.com/Lushenggang/show-pdf https://github.com/Lushenggang/show-pdf

  4. web中浏览PDF文件

    1.在web中浏览pdf文件. 2.支持大多数主流浏览器,包括IE8 3.参考网址: https://pdfobject.com/ http://mozilla.github.io/pdf.js/ & ...

  5. WPF 浏览PDF 文件

    添加成功后会在工具箱里看到下图所示的控件.打开VS2010,新建项目(WpfPDFReader),右键项目添加User Control(用户控件).因为Adobe PDF Reader COM 组件是 ...

  6. 在线浏览pdf文件,pdfobject的简单使用

    该js插件,官网有详细的使用教程(网址:http://www.pdfobject.com/examples/).打开里面的例子后,查看新打开页面,打开并查看该页面的源代码. 需要的材料: 1.PDFo ...

  7. ASP.NET MVC 项目直接预览PDF文件

    背景及需求 项目使用的是MVC4框架,其中有一个功能是根据设置生成PDF文件,并在点击时直接预览. 实现过程 1.第一版实现代码: HTML内容 @{ Layout = null; } <!DO ...

  8. 怎么用ABBYY在线浏览PDF文件

    ABBYY FineReader 让您可以从在线存储服务中打开图像或 PDF 文件,并将已识别文本保存至在线存储服务中,如 Dropbox.SkyDrive 或 Google Drive 等.通过在 ...

  9. 在asp.net mvc中上传大文件

    在asp.net mvc 页面里上传大文件到服务器端,需要如下步骤: 1. 在Control类里添加get 和 post 方法 // get method public ActionResult Up ...

随机推荐

  1. Java TreeMap 源码解析

    继上篇文章介绍完了HashMap,这篇文章开始介绍Map系列另一个比较重要的类TreeMap. 大家也许能感觉到,网络上介绍HashMap的文章比较多,但是介绍TreeMap反而不那么多,这里面是有原 ...

  2. IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)

    进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程 队列 dispa ...

  3. 1000万条数据导入mysql

    今天需要将一个含有1000万条数据的文本内容插入到数据库表中,最初自然想到的是使用Insertinto '表名'values(),(),()...这种插入方式,但是发现这种方式对1000万条数据量的情 ...

  4. Web 应用的安全性

    Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统.用户授权 ...

  5. Windows下环境变量配置

    JAVA_HOME=C:\Program Files\Java\jdk1.6.0_33   PATH+=%JAVA_HOME%\bin;   CLASSPATH=.;%JAVA_HOME%\lib\d ...

  6. Oracle- 表的自增长创建

    Oracle创建自增长要先写序列还要去写触发器,不像MSSQLSERVER那样方便.但也是麻烦,记录如下: Oracle中,可以为每张表的主键创建一个单独的序列,然后从这个序列中获取自动增加的标识符, ...

  7. SQL函数中的动态执行语句

    一.为什么要使用动态执行语句? 由于在PL/SQL 块或者存储过程中只支持DML语句及控制流语句,并不支持DDL语句,所以Oracle动态执行语句便应允而生了.关于DDL与DML的区别,请参见:DDL ...

  8. linux教程:[4]配置Tomcat开机启动

    http://jingyan.baidu.com/article/6525d4b1382f0aac7d2e9421.html 方法/步骤 1 请自行下载安装配置tomcat的服务器环境 本经验仅仅介绍 ...

  9. Struts数据效验

    表单数据的验证: 前台验证:主要是通过JS验证, 表达数据是否合法! 后台验证:通过后台java代码进行验证! Struts也提供了数据效验的方式! Struts数据效验, 通过拦截器完成: < ...

  10. Android Error

    [2016-03-27 13:00:51 - XWeChat] Dx warning: Ignoring InnerClasses attribute for an anonymous inner c ...