/*
我修改了一天时间.终于找到门路了。呵呵
access中存放文件内容的字段类型为:ole对象
sql中存放文件内容的字段类型为:image
此代码为上传文件代码.梢后整理发布下载文件代码
代码设计实现功能:asp.net上传文件到指定文件夹,access数据库,sql数据库代码
已经测试文件格式 .txt,jpg..mdb.gif
*/
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.io;
using system.data.oledb;
using system.data.sqlclient;
namespace webapplication1.managefile
{
/// <summary>
/// manageuploadfile 的摘要说明。
/// </summary>
public class manageuploadfile : system.web.ui.page
{
protected system.web.ui.webcontrols.dropdownlist dropdownlist1;
protected system.web.ui.webcontrols.button button2;
protected system.web.ui.htmlcontrols.htmlinputfile file1;
protected system.web.ui.webcontrols.textbox textbox1;
protected system.web.ui.webcontrols.checkbox checkbox1;
protected system.web.ui.webcontrols.button button4;
protected system.web.ui.webcontrols.button button5;
protected system.web.ui.webcontrols.button button3;
private void page_load(object sender, system.eventargs e)
{
// 在此处放置用户代码以初始化页面
}
#region web 窗体设计器生成的代码
override protected void oninit(eventargs e)
{
//
// codegen: 该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.button2.click += new system.eventhandler(this.button2_click);
this.button3.click += new system.eventhandler(this.button3_click);
this.button4.click += new system.eventhandler(this.button4_click);
this.button5.click += new system.eventhandler(this.button5_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void button1_serverclick(object sender, system.eventargs e)
{
if(file1.postedfile.filename!="")
{
if(checkbox1.checked)
{
}
else
{
//上传文件到数据库中
string suploadfilename=file1.postedfile.filename;
string struploadfile=server.mappath(".")+"//"+dropdownlist1.selecteditem.text.tostring()+"//";
suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("//")).replace("//","");
string suploadfilepath=struploadfile+suploadfilename;
int suploadfilelength=file1.postedfile.contentlength;
string suploadfiletype=file1.postedfile.contenttype.tostring();
//appdomain.currentdomain.basedirectory.tostring()站点跟目录
//file1.postedfile.saveas(suploadfilepath);
system.byte[] docbuffer = new byte[suploadfilelength];
stream objstream = file1.postedfile.inputstream;
objstream.read(docbuffer,0,suploadfilelength);
string dbname=server.mappath(".")+"//database//htmlfile.mdb";
string connectionstring ="provider=microsoft.jet.oledb.4.0;data source=" + dbname;
string accesssqlstring="insert into uploadfiles(uploadfilename,uploadfiletype,uploadfilepath,uploadfilelength,uploadfilecontent) values('" +suploadfilename+ "','"+suploadfiletype+ "','" +suploadfilepath+ "',"+suploadfilelength+ ","+ docbuffer + ")";
oledbconnection myconnection =new oledbconnection(connectionstring);
myconnection.open();
oledbcommand mycommand =new oledbcommand(accesssqlstring,myconnection);
mycommand.executenonquery();
myconnection.close();
string strupfileresult="上传文件到数据库成功/r/n";
strupfileresult=strupfileresult+"文件名"+suploadfilename+"/r/n";
strupfileresult=strupfileresult+"文件大小"+suploadfilelength+"/r/n";
strupfileresult=strupfileresult+"文件格式"+suploadfiletype+"/r/n";
textbox1.text=strupfileresult;
}
}
}
private void button2_click(object sender, system.eventargs e)
{
string strcurrentdirectory=server.mappath(".");
directoryinfo di=new directoryinfo(strcurrentdirectory);
int dtotal=0;
if(dropdownlist1.items.count!=0)
{
for(int j=dropdownlist1.items.count-1;j>=0;j--)
{
dropdownlist1.items.removeat(j);
}
}
for(int i=0;i<di.getdirectories().length;i++)
{
//subd=subd+"<br>"+di.getdirectories().getvalue(i);
dtotal=dtotal+1;
dropdownlist1.items.add(di.getdirectories().getvalue(i).tostring());
}
}
private void button3_click(object sender, system.eventargs e)
{
string suploadfilename=file1.postedfile.filename;
string struploadfile=server.mappath(".")+"//"+dropdownlist1.selecteditem.text.tostring()+"//";
suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("//")).replace("//","");
string spath=struploadfile+suploadfilename;
//appdomain.currentdomain.basedirectory.tostring()站点跟目录
file1.postedfile.saveas(spath);
string suploadfilelength=file1.postedfile.contentlength.tostring();
string suploadfiletype=file1.postedfile.contenttype.tostring();
string strupfileresult="上传文件成功/r/n";
strupfileresult=strupfileresult+"文件名"+suploadfilename+"/r/n";
strupfileresult=strupfileresult+"文件大小"+suploadfilelength+"/r/n";
strupfileresult=strupfileresult+"文件格式"+suploadfiletype+"/r/n";
textbox1.text=strupfileresult;
}
private void button4_click(object sender, system.eventargs e)
{
//上传文件到数据库中
string suploadfilename=file1.postedfile.filename;
string struploadfile=server.mappath(".")+"//"+dropdownlist1.selecteditem.text.tostring()+"//";
suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("//")).replace("//","");
string suploadfilepath=struploadfile+suploadfilename;
int suploadfilelength=file1.postedfile.contentlength;
string suploadfiletype=file1.postedfile.contenttype.tostring();
//appdomain.currentdomain.basedirectory.tostring()站点跟目录
//file1.postedfile.saveas(suploadfilepath);
system.byte[] docbuffer = new byte[suploadfilelength];
stream objstream = file1.postedfile.inputstream;
objstream.read(docbuffer,0,suploadfilelength);
string dbname=server.mappath(".")+"//database//htmlfile.mdb";
string connectionstring ="provider=microsoft.jet.oledb.4.0;data source=" + dbname;
//string accesssqlstring="insert into uploadfiles(uploadfilename,uploadfiletype,uploadfilepath,uploadfilelength) values('" +suploadfilename+ "','"+suploadfiletype+ "','" +suploadfilepath+ "',"+suploadfilelength+")";
string accesssqlstring="insert into uploadfiles(uploadfilename,uploadfiletype,uploadfilepath,uploadfilelength,uploadfilecontent) values('" +suploadfilename+ "','"+suploadfiletype+ "','" +suploadfilepath+ "',"+suploadfilelength+ ",'"+ docbuffer + "')";
oledbconnection myconnection =new oledbconnection(connectionstring);
myconnection.open();
oledbcommand mycommand =new oledbcommand(accesssqlstring,myconnection);
mycommand.executenonquery();
myconnection.close();
string strupfileresult="上传文件到数据库成功/r/n";
strupfileresult=strupfileresult+"上传文件名"+suploadfilename+"/r/n";
strupfileresult=strupfileresult+"上传文件大小"+suploadfilelength+"/r/n";
strupfileresult=strupfileresult+"上传文件路径"+suploadfilepath+"/r/n";
strupfileresult=strupfileresult+"上传文件格式"+suploadfiletype+"/r/n";
textbox1.text=strupfileresult;
}
private void button5_click(object sender, system.eventargs e)
{
//上传文件到数据库中
string suploadfilename=file1.postedfile.filename;
string struploadfile=server.mappath(".")+"//"+dropdownlist1.selecteditem.text.tostring()+"//";
suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("//")).replace("//","");
string suploadfilepath=struploadfile+suploadfilename;
int suploadfilelength=file1.postedfile.contentlength;
string suploadfiletype=file1.postedfile.contenttype.tostring();
//appdomain.currentdomain.basedirectory.tostring()站点跟目录
//file1.postedfile.saveas(suploadfilepath);
system.byte[] docbuffer = new byte[suploadfilelength];
stream objstream = file1.postedfile.inputstream;
objstream.read(docbuffer,0,suploadfilelength);
//string strcon ="initial catalog=northwind;data source=localhost;integrated security=sspi;";
//uid=sa,pwd=hnxqf222,server=127.0.0.1,database=aspnet";
/*
sqlcommand mycommand = new sqlcommand(myexecutequery, myconnection);
mycommand.connection.open();
mycommand.executenonquery();
myconnection.close();
*/
string strconn ="database=aspuser;server=localhost;uid=sa;pwd=hnxqf222;";
sqlconnection conn = new sqlconnection(strconn);
conn.open();
string mysqlcommand = "insert into uploadfiles (uploadfilename,uploadfiletype,uploadfilepath,uploadfilelength,uploadfilecontent) values (@uploadfilename,@uploadfiletype,@uploadfilepath,@uploadfilelength,@uploadfilecontent)";
sqlcommand cmdobj = new sqlcommand(mysqlcommand, conn);
cmdobj.parameters.add("@uploadfilename",sqldbtype.varchar,50).value = suploadfilename;
cmdobj.parameters.add("@uploadfiletype",sqldbtype.varchar,50).value = suploadfiletype;
cmdobj.parameters.add("@uploadfilepath",sqldbtype.varchar,200).value = suploadfilepath;
cmdobj.parameters.add("@uploadfilelength",sqldbtype.bigint,8).value = suploadfilelength;
cmdobj.parameters.add("@uploadfilecontent",sqldbtype.image).value = docbuffer;
cmdobj.executenonquery();
conn.close();
string strupfileresult="上传文件到数据库成功/r/n";
strupfileresult=strupfileresult+"上传文件名"+suploadfilename+"/r/n";
strupfileresult=strupfileresult+"上传文件大小"+suploadfilelength+"/r/n";
strupfileresult=strupfileresult+"上传文件路径"+suploadfilepath+"/r/n";
strupfileresult=strupfileresult+"上传文件格式"+suploadfiletype+"/r/n";
textbox1.text=strupfileresult;
}
}
}
新闻热点
疑难解答
图片精选