Create Table tupdoc
(
Docid int not null identity primary key,
Doctitle varchar (200 ),
Doc image,
Doctype varchar (50 ),
Entrydate datetime default getdate ()
)
----------------------------------------------------------
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. Data. sqlclient;
Using system. IO;
Namespace longmengoa
{
/// <Summary>
/// Summary of upfile.
/// </Summary>
Public class upfile: system. Web. UI. Page
{
Protected system. Web. UI. htmlcontrols. htmlinputfile ftpfiles;
Protected system. Web. UI. webcontrols. textbox txtfilename;
Protected system. Web. UI. webcontrols. Button btnupfile;
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. btnupfile. Click + = new system. eventhandler (this. btnupfile_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void btnupfile_click (Object sender, system. eventargs E)
{
// If (ftpfiles. postedfile! = NULL)
//{
// Try
//{
// Ftpfiles. postedfile. saveas ("C: \" + textboxfilename. Text );
// Labelstat. Text = "File Uploaded successfully! ";
//}
// Catch (exception exc)
//{
// Labelstat. Text = "An error occurred while uploading! "+ Exc. tostring ();
//}
//}
String strdocext;
// Strdoctype is used to save the type of the uploaded file.
String strdoctype;
String filetitle = txtfilename. text;
// Used to save the file size
Int intdoclen;
// Stream is used to read uploaded data.
Stream objstream;
Sqlconnection objconnection;
Sqlcommand cmduploaddoc;
If (ftpfiles. postedfile! = NULL)
{
// File type
// Strdocext = ftpfiles. postedfile. filename. GetType. tolower ();
String filename = path. getfilename (ftpfiles. postedfile. filename );
Strdocext = system. Io. Path. getextension (filename );
Switch (strdocext)
{
Case ". Doc ":
Strdoctype = "Doc ";
Break;
Case ". ppt ":
Strdoctype = "ppt ";
Break;
Case ". htm ":
Strdoctype = "htm ";
Break;
Case ". html ":
Strdoctype = "htm ";
Break;
Case ". jpg ":
Strdoctype = "jpg ";
Break;
Case ". GIF ":
Strdoctype = "GIF ";
Break;
Default:
Strdoctype = "TXT ";
Break;
}
// Upload the object content
Intdoclen = ftpfiles. postedfile. contentlength;
Byte [] docbuffer = new byte [intdoclen];
Objstream = ftpfiles. postedfile. inputstream;
// Save the file to the cache
// The cache will be saved to the database
Objstream. Read (docbuffer, 0, intdoclen );
String connstr = "Server = (local); initial catalog = longmengoa; user id = sa; Password = lm ;";
// String sqlstr = "insert into tupdoc (doctitle, Doc, doctype) values ('" + filetitle + "'," + docbuffer + ", '" + strdoctype + "') ";
String sqlstr = "insert into tupdoc (doctitle, Doc, doctype) values (@ title, @ doc, @ doctype )";
Objconnection = new sqlconnection (connstr );
Cmduploaddoc = new sqlcommand (sqlstr, objconnection );
Cmduploaddoc. Parameters. Add ("@ title", sqldbtype. varchar, 50 );
Cmduploaddoc. Parameters. Add ("@ Doc", sqldbtype. Image );
Cmduploaddoc. Parameters. Add ("@ doctype", sqldbtype. varchar, 4 );
Cmduploaddoc. Parameters ["@ title"]. value = filetitle;
Cmduploaddoc. Parameters ["@ Doc"]. value = docbuffer;
Cmduploaddoc. Parameters ["@ doctype"]. value = strdoctype;
Objconnection. open ();
Cmduploaddoc. executenonquery ();
Objconnection. Close ();
Response. Write ("<script language = JavaScript> alert ('uploaded successfully'); </SCRIPT> ");
}
}
}
}