本文主要讲解在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. 基于memcached的单机轻量级通用缓存库minicached的实现

    一.前言 之前拜读过淘宝子柳的<淘宝技术这十年>之大作,深知缓存技术在系统优化中起着一个举足轻重的作用.无论是文件系统静态文件,数据库的访问,乃至网络数据的请求,只要是与内存访问速度相差较 ...

  2. 【现代程序设计】【homework-02】【11061027】

    Q:描述在这么多相似的需求面前, 你怎么维护你的设计 (父类/子类/基类, UML, 设计模式,  或者其它方法) 让整个程序的架构不至于崩溃的? A:由于使用的是面向过程的C语言,所以维护设计这个问 ...

  3. INPUT输入框灰体提示

    INPUT输入框灰体提示 <input type="text" value='15 words limit' style="color:#999999" ...

  4. SQL2008-表对表直接复制数据

    1.全部复制,使用简单,但是字段容易出错(字段和顺序必须相同)  INSERT INTO AAAStuffAgitationYield SELECT * FROM StuffAgitationYiel ...

  5. if/else语句匹配问题

    1: if(*FixedMode == LNG_OUT_FIXED_AMOUNT){ 2: if(NumTemp > MIN_FIXED_AMOUNT && NumTemp &l ...

  6. ModSecurity for Nginx

    Announcing the availability of ModSecurity extension for Nginx ModSecurity for Nginx ModSecurity for ...

  7. 关于iOS中SQLITE句柄的使用的细节

    1.设计思想:给SQLITE进行封装,利用定义的类别实现方法的简洁,以便达到低耦合效果 控制器代码: #import "ViewController.h" #import &quo ...

  8. 禁止UINavigationController 左滑 返回的效果

    在iOS7中,新增加了一个小小的功能,也就是这个:self.navigationController.interactivePopGestureRecognizer.enabled = YES;

  9. javafx for android or ios ?

    javafx是否支持android 或者 ios这是一个令人感兴趣的话题.google一番,发现有可行方案: 1. javafx on android: 两种方案:(事实上差点儿相同) 1.有位大神已 ...

  10. Android动画Animation之Tween用代码实现动画

    透明度动画.旋转动画.尺寸伸缩动画.移动动画 package com.javen.tween; import android.annotation.SuppressLint; import andro ...