asp⽹页中上传并且浏览pdf⽂件的实现本⽂主要讲解在asp中的gridview中浏览pdf⽂件。下⾯来看⼀下具体的实现:
第⼀步,使⽤sqlserver 创建⼀个数据库表。
第⼆步,新建⼀个webform,命名为uploadpdf.aspx。
第三步,在该页⾯中添加⼀个upload控件,两个button控件,代码如下。
<asp:fileupload ID="Fileupload1" runat="server"></asp:fileupload>
<asp:Button ID="Btnupload" runat="server" Text="上传"οnclick="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, 0, 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();
}
asp网页文件的格式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[2048];
int byteseq = strm.Read(buffer,0,2048);
while (byteseq > 0)
{
context.Response.OutputStream.Write(buffer, 0, byteseq);
byteseq = strm.Read(buffer, 0, 2048);
}
}
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();
}
}
View Code
第⼋步,这时应该在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连接,这时结果如下。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论