/*
I modified it for a day and finally found the path. Haha
The field type of the file content stored in access is OLE object.
The field type of file content stored in SQL is: Image
This code is the code for uploading files.
Code design implementation function: ASP. NET uploads files to a specified folder, Access database, SQL database code
The file format is. txt,. jpg,. mdb, And. 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 of 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 the page
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content 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 button#serverclick (Object sender, system. eventargs E)
{
If (file1.postedfile. filename! = "")
{
If (checkbox1.checked)
{
}
Else
{
// Upload a file 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 = "succeeded in uploading files to the database/R/N ";
Strupfileresult = strupfileresult + "file name" + 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 = "File Uploaded successfully/R/N ";
Strupfileresult = strupfileresult + "file name" + 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)
{
// Upload a file 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 = "succeeded in uploading files to the database/R/N ";
Strupfileresult = strupfileresult + "Upload File Name" + 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)
{
// Upload a file 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, folder, uploadfilecontent) values (@ uploadfilename, @ uploadfiletype, @ uploadfilepath, @ folder, @ 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 = "succeeded in uploading files to the database/R/N ";
Strupfileresult = strupfileresult + "Upload File Name" + 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;
}
}
}