本文主要讲解在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. 垃圾回收(GC)的三种基本方式

    垃圾(Garbage)就是程序需要回收的对象,如果一个对象不在被直接或间接地引用,那么这个对象就成为了「垃圾」,它占用的内存需要及时地释放,否则就会引起「内存泄露」.有些语言需要程序员来手动释放内存( ...

  2. AutoLayout框架Masonry使用心得

    AutoLayout框架Masonry使用心得 字数1769 阅读1481 评论1 喜欢17 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起 ...

  3. jQuery语法总结及注意事项

    1.关于页面元素的引用通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用dom ...

  4. 3.x的触摸响应机制

    第一种是采用函数回调,主要是用于MenuItem [cpp] view plaincopy // a selector callback void menuCloseCallback(Object*  ...

  5. 编写一个Animal类,具有属性:种类;具有功能:吃、睡。定义其子类Fish 和Dog,定义主类E,在其main方法中分别创建其对象并测试对象的特性。

    package animal; public class Animal { //成员属性 private String kind; public String getKind() { return k ...

  6. cocos2d-x 判断点击命中坐标的几种方法

    转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019893.html //重载 virtual bool ccTouchBegan(CC ...

  7. 如何编写程序设置Android来电铃声

    我们在拿到新手机后通常会为其设置来年铃声,那么怎样通过代码来设置Android来电铃声,本文就为大家实例讲解下. 1.如果读到的是音频文件路径,需要先将音乐文件插入到多媒体库. Java代码 //设置 ...

  8. SQL Server 2008中的hierarchyid

    这也是SQL Server 2008的一个重要新增特性.主要解决的问题是拥有层次关系的表格.例如我们日常生活中用到最多的组织结构图.我们一般会用一个Region表保存区域数据,而每个区域则又可能会有相 ...

  9. [AngularJS] 'require' prop in Directive or Component

    When use 'require', recommend to add some error check, for example: class ChildCtrl { constructor(){ ...

  10. Moving From Objective-C to C++

    1. virtual method or not: It's better to declare all cpp member methods without "virtual" ...