asp.net网页中上传并且浏览pdf文件的实现

本文主要讲解在asp.net中的gridview中浏览pdf文件。下面来看一下具体的实现:

第一步,使用sqlserver 创建一个数据库表。

asp.net网页中上传并且浏览pdf文件的实现

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

asp.net网页中上传并且浏览pdf文件的实现

第三步,在该页面中添加一个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" />

asp.net网页中上传并且浏览pdf文件的实现

第四步,单击上传按钮在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文件。

asp.net网页中上传并且浏览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+""); }

到这里就大功告成,然后进行测试。

测试结果如下

asp.net网页中上传并且浏览pdf文件的实现

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

asp.net网页中上传并且浏览pdf文件的实现

上一篇:Erlang进程的Link机制


下一篇:THEPYTHONCHALLENG闯关记录