仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1315|回复: 19
打印 上一主题 下一主题

[学习教程] ASP网页编程之用ado.net对word,excel停止存取

[复制链接]
冷月葬花魂 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:38:21 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
ASP最大的缺点在于网络的安全性和可靠性,企业将经营数据放在开放的平台上,最大的担忧就是如何保证这些数据不被其他人破坏。ado|excel|word   blob表

3 id int 4 0
0 name char 50 1
0 blob image 16 1
0 type char 60 1

saveFile.aspx.cs

private void Button1_Click(object sender, System.EventArgs e)
{
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string name = this.getFileNameByURL(this.File1.PostedFile.FileName);
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
string connstr = "workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )", connection );
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );
paramTitle.Value = name;
command.Parameters.Add(paramTitle);
SqlParameter paramData = new SqlParameter( "@blob", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter( "@type", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );
wztj.debug.TestSQL.TraceErrorSql("INSERT INTO blob(name,type,blob) VALUES ( @imgtitle, @type,@blob )",command.Parameters);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}

listFile.aspx//这个器材次要用来列表,把已有的器材列出来

<asp:HyperLinkColumn DataNavigateUrlField="id" HeaderText="产物称号" DataNavigateUrlFormatString="./getFile.aspx?ID={0}" DataTextField="name" DataTextFormatString="{0}" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="160px">

listFile.aspx.cs

string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("select * from blob", connection );
connection.Open();
SqlDataAdapter adaptor = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adaptor.Fill(ds,"blob");
connection.Close();
this.DataGrid1.DataSource=ds.Tables["blob"].DefaultView;
this.DataGrid1.DataBind();

getFile.aspx.cs//这个文件对照主要担任把村道数据库外面的文件,依照格局,依照称号,给传输入来

private void Page_Load(object sender, System.EventArgs e)
{
string imgid =this.Request.QueryString.Get("ID");
//Request.QueryString["imgid"];
string connstr="workstation id=OVERMIND;packet size=4096;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj";
string sql="SELECT name,blob, type FROM blob WHERE id = " + imgid;
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输入流为简体中文
//Response.ContentType = "application/ms-word";//设置输入文件类型为word文件。
Response.ContentType = dr["type"].ToString();
Response.BinaryWrite( (byte[]) dr["blob"] );
string FileName = dr["name"].ToString().Trim();
FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );
Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
}
connection.Close();
}


这里要说的有两点,第一,就是把文件的称号getFile.aspx酿成咱们想要的称号。

Response.AppendHeader("Content-Disposition", "attachment;filename="+FileName);

第二,就是把指定的称号酿成咱们想要的值,是尺度的中文,而不是中文的乱码。

FileName=System.Web.HttpUtility.UrlEncode(FileName,System.Text.Encoding.UTF8 );
</p>  无法实现跨操作系统的应用。当然这也是微软的理由之一,只有这样才能发挥ASP最佳的能力。可是我却认为正是Windows限制了ASP,ASP的概念本就是为一个能让系统运行于一个大的多样化环境而设计的;
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-18 13:12

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表