我们可以把任意类型的文件保存到sql server中,在进行例子之前,先建立测试用表格,testfile.sql:
if exists (select * from dbo.sysobjects where id = object_id(n[dbo].[testfiles])
and objectproperty(id, nisusertable) = 1)
drop table [dbo].[testfiles]
go
create table [dbo].[testfiles] (
[id] [int] identity (1, 1) not null ,
[myfilename] [varchar] (50) collate chinese_prc_ci_as not null ,
[filetype] [varchar] (50) collate chinese_prc_ci_as not null ,
[myfile] [image] not null
) on [primary] textimage_on [primary]
go
下面创建上传表单:
一旦提交了表单,我们使用htmlinputfile类的postedfile属性来访问我们上载的文件,用httppostedfile类的属性和方法来进行读取、保存上载文件和得到上载文件的其它信息。这里我们不使用saveas方法,因为它是用来保存文件的。我们要把数据保存到数据库中,我们使用inputstream属性,它用来初始化流来读取我们的数据。同时,我们使用contentlength来读取文件大小,contenttype读取文件类型。然后创建byte数组,把文件流保存进该数组,然后保存到数据库即可。
下面就是完整的代码【cs版本】uploadfile.aspx:
<% @page language="c#" %>
<% @import namespace="system.io" %>
<% @ import namespace="system.data" %>
<% @ import namespace="system.data.sqlclient" %>
<script runat="server">
public void uploadbtn_click (object sender, eventargs e){
//得到提交的文件
stream filedatastream = myfile.postedfile.inputstream;
//得到文件大小
int filelength = myfile.postedfile.contentlength;
//创建数组
byte[] filedata = new byte[filelength];
//把文件流填充到数组
filedatastream.read(filedata,0,filelength);
//得到文件名字
string filetitle = myfilename.value;
//得到文件类型
string filetype = myfile.postedfile.contenttype;
//构建数据库连接,sql语句,创建参数
sqlconnection connection = new sqlconnection("server=.;uid=sa;pwd=;database=testuploadfile");
sqlcommand command = new sqlcommand ("insert into testfiles (myfilename,myfile,filetype)" +
"values (@myfilename,@myfile,@filetype)", connection);
sqlparameter paramtitle = new sqlparameter ("@myfilename", sqldbtype.varchar,35);
paramtitle.value = filetitle;
command.parameters.add(paramtitle);
sqlparameter paramdata = new sqlparameter ("@myfile", sqldbtype.image);
paramdata.value = filedata;
command.parameters.add(paramdata);
sqlparameter paramtype = new sqlparameter ("@filetype", sqldbtype.varchar,25);
paramtype.value = filetype;
command.parameters.add(paramtype);
//打开连接,执行查询
connection.open();
command.executenonquery();
connection.close();
message.text="你的文件已经成功上载";
myfilename.value = "";
}
</script>
<hr>
<asp:label id="message" text="选择文件和文件名字:" runat="server"/>
<hr>
<form method="post" enctype="multipart/form-data" runat="server">
<b>文件名字:</b><input id="myfilename" type="text" runat="server">
<p>
<b>文件:</b><input id="myfile" type="file" runat="server">
<br><br>
<input type=submit value="开始上传" onserverclick="uploadbtn_click" runat="server">
</form>
一旦我们上载成功,我们可以对文件进行浏览:只需要设置页面的mime类型,然后用response对象的binarywrite()进行输出。
showuploadfile.aspx
<% @page language="c#" %>
<% @import namespace="system.io" %>
<% @ import namespace="system.data" %>
<% @ import namespace="system.data.sqlclient" %>
<script runat="server">
private void page_load(object sender, eventargs e) {
string sql="select * from testfiles";
sqlconnection connection = new sqlconnection("server=.;uid=sa;pwd=;database=testuploadfile");
sqlcommand command = new sqlcommand(sql, connection);
connection.open();
filelist.datasource = command.executereader();
filelist.databind();
connection.close();
}
</script>
<form runat="server">
<asp:datagrid id="filelist" runat="server"
bordercolor="orange" borderwidth="2" cellpadding="4"
autogeneratecolumns="false" showheader="true" align="center">
<headerstyle bordercolor="white" backcolor="black" forecolor="white"
font-bold="true" font-size="9" horizontalalign="center"/>
<columns>
<asp:templatecolumn headertext="文件名字">
<itemtemplate>
<b>
<%# databinder.eval(container.dataitem, "myfilename") %>
</b>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="类型">
<itemtemplate>
<b>
<%# databinder.eval(container.dataitem, "filetype") %>
</b>
</itemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="查看">
<itemtemplate>
<b>
<a href="showfile.aspx?id=<%# databinder.eval(container.dataitem, id) %>">查看文件</a>
</b>
</itemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
showfile.aspx
<% @page language="c#" %>
<% @import namespace="system.io" %>
<% @ import namespace="system.data" %>
<% @ import namespace="system.data.sqlclient" %>
<script runat="server">
private void page_load(object sender, eventargs e) {
string sql="select * from testfiles where id = " + request.querystring["id"] + "";
sqlconnection connection = new sqlconnection("server=.;uid=sa;pwd=;database=testuploadfile");
sqlcommand command = new sqlcommand(sql, connection);
connection.open();
sqldatareader dr = command.executereader();
if(dr.read()){
response.clear();
response.addheader("content-type",dr["filetype"].tostring());
response.binarywrite((byte[])dr["myfile"]);
}
dr.close();
connection.close();
}
</script>
另外需要注意的是:对exe,zip文件等还要进一步进行处理,以直接进行下载。
