asp.NET上传文件到指定文件夹,ACCESS数据库,SQL数据库代码

80酷酷网    80kuku.com

  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 窗体设计器生成的代码
  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(".")+"";
     sUploadFileName=sUploadFileName.Substring(sUploadFileName.LastIndexOf("","");
     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(".")+"";
     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+"
"+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(".")+"";
   sUploadFileName=sUploadFileName.Substring(sUploadFileName.LastIndexOf("","");
   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(".")+"";
   sUploadFileName=sUploadFileName.Substring(sUploadFileName.LastIndexOf("","");
   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(".")+"";
   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(".")+"";

   sUploadFileName=sUploadFileName.Substring(sUploadFileName.LastIndexOf("","");
   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;
  }
 }
}
 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: