Monday, January 28, 2008

文件上传的程序

引用输入输出命名空间using System.IO;

类型绑定,因为类型是可以后台更改的,不是固定的
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
TypeBind();
}
private void TypeBind()
{
try
{
string DbName =Server.MapPath(@"..\db\Sources.mdb");
string ConnSourcesStr ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + DbName;
Conn =new System.Data.OleDb.OleDbConnection(ConnSourcesStr);
Conn.Open();
string strb="select * from bType order by ID";
System.Data.OleDb.OleDbCommand Cmd =new System.Data.OleDb.OleDbCommand(strb ,Conn);
System.Data.OleDb.OleDbDataReader Drb =Cmd.ExecuteReader();
while(Drb.Read())
{
this.ddl_btype.Items.Add(Drb["bTypeName"].ToString());
this.ddl_stype.Items.Add("----"+ Drb["bTypeName"].ToString() +"----");

System.Data.OleDb.OleDbConnection conn =new System.Data.OleDb.OleDbConnection(ConnSourcesStr);
conn.Open();
string strs ="select * from sType where bTypeID =" + Convert.ToInt16(Drb["ID"]);
System.Data.OleDb.OleDbCommand cmd =new System.Data.OleDb.OleDbCommand(strs ,conn);
System.Data.OleDb.OleDbDataReader Dr =cmd.ExecuteReader();
while(Dr.Read())
{
this.ddl_stype.Items.Add(Dr["sTypeName"].ToString());
}
Dr.Close();
}
Drb.Close();
Conn.Close();
}
catch(Exception ex)
{
Response.Write(" ex.Message.ToString() ");
}
}

上传文件,这里根据日期,每半个月建立一个文件夹
private void btn_fileUp_Click(object sender, System.EventArgs e)
{
if(this.tb_net.PostedFile !=null)
{
try
{
string path=Server.MapPath(@"..\UploadFile\down\");
string dir,filename,chldir;
int n;
string day =DateTime.Now.Day.ToString();

if( Convert.ToInt16(day) < 16)
{
dir=path +DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +"-1";
bool dirExit =System.IO.Directory.Exists(dir);
if(! dirExit)
{
System.IO.Directory.CreateDirectory(dir);
}
n =this.tb_net.PostedFile.FileName.LastIndexOf("\\") +1;
chldir =DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +"-1";
filename =DateTime.Now.ToShortDateString().Replace(@"-","") +this.tb_net.PostedFile.FileName.Substring(n);
}
else
{
dir=path + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +"-2";
bool dirExit =System.IO.Directory.Exists(dir);
if(! dirExit)
{
System.IO.Directory.CreateDirectory(dir);
}
n =this.tb_net.PostedFile.FileName.LastIndexOf("\\") +1;
chldir = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() +"-2";
filename =DateTime.Now.ToShortDateString().Replace(@"-","") +this.tb_net.PostedFile.FileName.Substring(n);
}
this.tb_net.PostedFile.SaveAs(path + chldir+"\\"+ filename);

this.tb_name.Text =filename;
this.tb_size.Text =this.tb_net.PostedFile.ContentLength.ToString();
this.tb_down1.Text
=@"UploadFile/down/" + chldir +@"/"+ filename;
Response.Write(" ");
}
catch(Exception ex)
{
Response.Write ex.Message.ToString() ;
}
}
}

这里是添加记录到数据库,我这里用的是access,需要注意的就是更新的sql语句。
private void btn_OK_Click(object sender, System.EventArgs e)
{
string fname =this.tb_name.Text.Trim();
string flng =this.ddl_lng.SelectedItem.Text.Trim();
string fbtype =this.ddl_btype.SelectedItem.Text.Trim();
string fstype =this.ddl_stype.SelectedItem.Text;
string fsize =this.tb_size.Text;
string fmode =this.ddl_mode.SelectedItem.Text;
string fvalue =this.ddl_value.SelectedItem.Text;
string findate =DateTime.Now.ToShortDateString();
string fshow =this.tb_show.Text;
string fdown1 =this.tb_down1.Text;
string fdown2 =this.tb_down2.Text;
string fdown3 =this.tb_down3.Text;
string fpic1 =picpath1.Text;
string fpic2 =picpath2.Text;
string fpic3 =picpath3.Text;
try
{
string DbName =Server.MapPath(@"..\db\Sources.mdb");
string ConnSourcesStr ="PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + DbName;
Conn =new System.Data.OleDb.OleDbConnection(ConnSourcesStr);
Conn.Open();
string instr ="INSERT INTO download (Dname,Dintime,Dbtype,Dstype,Dmode,Dlanguage,Dvalue,Dsize,Dpic,Dpic2,Dpic3,Dintroduce,down1,down2,down3) VALUES ('"
+ fname +"','" +findate +"','" +fbtype +"','" +fstype +"','" +fmode +"','" +flng +"','" +fvalue +"','" +fsize +"','" +fpic1
+"','"+fpic2+"','"+fpic3 +"','" + fshow +"','" +fdown1 +"','" +fdown2 +"','" +fdown3 +"')";
Cmd =new System.Data.OleDb.OleDbCommand(instr,Conn);
Cmd.ExecuteNonQuery();
Response.Write(" ");
Conn.Close();
}
catch(Exception ex)
{
Response.Write(" ex.Message.ToString() ");
}
}


Related Posts



0 Comments:

 

gf

rgdfgfdg

gf

gf

gf