/*
我修改了一天時間.終于找到門路了。呵呵
access中存放文件內(nèi)容的字段類型為:ole對象
sql中存放文件內(nèi)容的字段類型為:image
此代碼為上傳文件代碼.梢后整理發(fā)布下載文件代碼
代碼設(shè)計實(shí)現(xiàn)功能:asp.net上傳文件到指定文件夾,access數(shù)據(jù)庫,sql數(shù)據(jù)庫代碼
已經(jīng)測試文件格式 .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 窗體設(shè)計器生成的代碼
  override protected void oninit(eventargs e)
  {
   //
   // codegen: 該調(diào)用是 asp.net web 窗體設(shè)計器所必需的。
   //
   initializecomponent();
   base.oninit(e);
  }
  
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內(nèi)容。
  /// </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
    {
     //上傳文件到數(shù)據(jù)庫中
     string suploadfilename=file1.postedfile.filename; 
     string struploadfile=server.mappath(".")+"http://"+dropdownlist1.selecteditem.text.tostring()+"http://";
     suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("http://")).replace("http://",""); 
     string suploadfilepath=struploadfile+suploadfilename;      
     int suploadfilelength=file1.postedfile.contentlength;
     string suploadfiletype=file1.postedfile.contenttype.tostring();   
     
     //appdomain.currentdomain.basedirectory.tostring()站點(diǎn)跟目錄
     //file1.postedfile.saveas(suploadfilepath);
     system.byte[] docbuffer = new byte[suploadfilelength]; 
     stream objstream = file1.postedfile.inputstream; 
     objstream.read(docbuffer,0,suploadfilelength); 
     
     string dbname=server.mappath(".")+"http://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="上傳文件到數(shù)據(jù)庫成功/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(".")+"http://"+dropdownlist1.selecteditem.text.tostring()+"http://";
   suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("http://")).replace("http://",""); 
   string spath=struploadfile+suploadfilename; 
   //appdomain.currentdomain.basedirectory.tostring()站點(diǎn)跟目錄
   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)
  {
  
   //上傳文件到數(shù)據(jù)庫中
   string suploadfilename=file1.postedfile.filename; 
   string struploadfile=server.mappath(".")+"http://"+dropdownlist1.selecteditem.text.tostring()+"http://";
   suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("http://")).replace("http://",""); 
   string suploadfilepath=struploadfile+suploadfilename;      
   int suploadfilelength=file1.postedfile.contentlength;
   string suploadfiletype=file1.postedfile.contenttype.tostring();   
     
   //appdomain.currentdomain.basedirectory.tostring()站點(diǎn)跟目錄
   //file1.postedfile.saveas(suploadfilepath);
   system.byte[] docbuffer = new byte[suploadfilelength]; 
   stream objstream = file1.postedfile.inputstream; 
   objstream.read(docbuffer,0,suploadfilelength); 
     
   string dbname=server.mappath(".")+"http://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="上傳文件到數(shù)據(jù)庫成功/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)
  {
   
   //上傳文件到數(shù)據(jù)庫中
   string suploadfilename=file1.postedfile.filename; 
   string struploadfile=server.mappath(".")+"http://"+dropdownlist1.selecteditem.text.tostring()+"http://";
   suploadfilename=suploadfilename.substring(suploadfilename.lastindexof("http://")).replace("http://",""); 
   string suploadfilepath=struploadfile+suploadfilename;      
   int suploadfilelength=file1.postedfile.contentlength;
   string suploadfiletype=file1.postedfile.contenttype.tostring();   
     
   //appdomain.currentdomain.basedirectory.tostring()站點(diǎn)跟目錄
   //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="上傳文件到數(shù)據(jù)庫成功/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;
  }
 }
}
 
新聞熱點(diǎn)
疑難解答
圖片精選