Asp. NET upload files to a specified folder, Access database, SQL database code

Source: Internet
Author: User
Tags count file size tostring access database
access|asp.net| Upload | data | database

/*

I modified the day. Finally found the way. Oh
The type of field in Access that holds the contents of the file is: OLE Object
The type of field in SQL that holds the contents of the file is: image
This code is for uploading file code. Post-Finishing release download file code

Code Design Implementation function: ASP. NET upload files to a specified folder, Access database, SQL database code

The file format has been tested. 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>
Summary description of the 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)
{
Place user code here to initialize page
}

   #region The code generated by the Web Forms Designer
  override protected void OnInit (EventArgs e)
  {
   //
   //codegen: This call is required for the ASP.net Web forms Designer.
   //
   initializecomponent ();
   base. OnInit (e);
  }
  
  ///<summary>
  ///Designer supports the desired method-do not use the Code Editor to modify
  /// The contents of this method.
  ///</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
{
Uploading files to the database
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 () Site and directory
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= "Upload file to database success \ r \ n";
strupfileresult=strupfileresult+ "filename" +suploadfilename+ "\ r \ n";
strupfileresult=strupfileresult+ "File size" +suploadfilelength+ "\ r \ n";
strupfileresult=strupfileresult+ "file format" +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 () Site and directory
File1. Postedfile.saveas (spath);
String Suploadfilelength=file1. PostedFile.ContentLength.ToString ();
String Suploadfiletype=file1. PostedFile.ContentType.ToString ();

String strupfileresult= "Upload file successfully \ r \ n";
strupfileresult=strupfileresult+ "filename" +suploadfilename+ "\ r \ n";
strupfileresult=strupfileresult+ "File size" +suploadfilelength+ "\ r \ n";
strupfileresult=strupfileresult+ "file format" +suploadfiletype+ "\ r \ n";

Textbox1.text=strupfileresult;
}

private void Button4_Click (object sender, System.EventArgs e)
{

Uploading files to the database
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 () Site and directory
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= "Upload file to database success \ r \ n";
strupfileresult=strupfileresult+ "Upload filename" +suploadfilename+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file size" +suploadfilelength+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file path" +suploadfilepath+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file format" +suploadfiletype+ "\ r \ n";
Textbox1.text=strupfileresult;

}

private void Button5_click (object sender, System.EventArgs e)
{

Uploading files to the database
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 () Site and directory
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= "Upload file to database success \ r \ n";
strupfileresult=strupfileresult+ "Upload filename" +suploadfilename+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file size" +suploadfilelength+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file path" +suploadfilepath+ "\ r \ n";
strupfileresult=strupfileresult+ "Upload file format" +suploadfiletype+ "\ r \ n";
Textbox1.text=strupfileresult;
}
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.