asp.NET上傳檔案到指定檔案夾,ACCESS資料庫,SQL資料庫代碼

來源:互聯網
上載者:User
access|asp.net|上傳|資料|資料庫

/*

我修改了一天時間.終於找到門路了。呵呵
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 Form設計器產生的程式碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web Form設計器所必需的。
   //
   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;
  }
 }
}
 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。