Principle: Read the file to be uploaded into a byte array! In the specified path on the server side, generate a file and write this byte array!
I. File. aspx file code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "FileUpLoad. aspx. cs" Inherits = "FileUpLoad" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> Publish Water Information </title>
<Link rel = "stylesheet" type = "text/css" href = "css/style.css"/>
</Head>
<Script type = "text/javascript">
SEP_PADDING = 5
HANDLE_PADDING = 7
Var yToolbars = new Array ();
Var YInitialized = false;
Var pureText = true
Var bodyTag = "
Public_description = new Editor
// Enable IFRAME to write values
Function document. onreadystatechange (){
HtmlEdit.doc ument. open ();
HtmlEdit.doc ument. write (bodyTag );
HtmlEdit.doc ument. close ();
HtmlEdit.doc ument. designMode = "On ";
}
Function InitBtn (btn)
{
Btn. onmouseover = BtnMouseOver;
Btn. onmouseout = BtnMouseOut;
Btn. onmousedown = BtnMouseDown;
Btn. onmouseup = BtnMouseUp;
Btn. YINITIALIZED = true;
Return true;
}
// Footer display
Function BtnMouseOver ()
{
If (event. srcElement. tagName! = "IMG") return false;
Var image = event. srcElement;
Var element = image. parentElement;
If (image. className = "Ico") element. className = "BtnMouseOverUp ";
Else if (image. className = "IcoDown") element. className = "BtnMouseOverDown ";
Event. cancelBubble = true;
}
Function BtnMouseOut ()
{
If (event. srcElement. tagName! = "IMG "){
Event. cancelBubble = true;
Return false;
}
Var image = event. srcElement;
Var element = image. parentElement;
YRaisedElement = null;
Element. className = "Btn ";
Image. className = "Ico ";
Event. cancelBubble = true;
}
Function BtnMouseDown ()
{
If (event. srcElement. tagName! = "IMG "){
Event. cancelBubble = true;
Event. returnValue = false;
Return false;
}
Var image = event. srcElement;
Var element = image. parentElement;
Element. className = "BtnMouseOverDown ";
Image. className = "IcoDown ";
Event. cancelBubble = true;
Event. returnValue = false;
Return false;
}
Function BtnMouseUp ()
{
If (event. srcElement. tagName! = "IMG "){
Event. cancelBubble = true;
Return false;
}
Var image = event. srcElement;
Var element = image. parentElement;
If (element. YUSERONCLICK) eval (element. YUSERONCLICK + "anonymous ()");
Element. className = "BtnMouseOverUp ";
Image. className = "Ico ";
Event. cancelBubble = true;
Return false;
}
// Font color
Function format (what, opt)
{
If (opt = "removeFormat ")
{
What = opt;
Opt = null;
}
If (opt = null) HtmlEdit.document.exe cCommand (what );
Else HtmlEdit.document.exe cCommand (what, "", opt );
PureText = false;
HtmlEdit. focus ();
}
Function foreColor ()
{
Var arr = document. all. dlgHelper. ChooseColorDlg ();
Arr = arr. toString (16 );
If (arr! = Null) format ('forecolor', arr );
Else HtmlEdit. focus ();
}
// Upload
Function test ()
{
Var str=HtmlEdit.doc ument. body. innerHTML;
Var str1 = document. getElementById ("FileUpLoad"). value;
Var str2 = document. getElementById ("txtTitle"). value;
Var str3 = document. getElementById ("txtUser"). value;
If (str1 = "")
{
Alert ("Select Upload File! ");
Document. getElementById ("FileUpLoad"). focus ();
Return;
}
If (str2 = "")
{
Alert ("Enter the file title! ");
Document. getElementById ("txtTitle"). focus ();
Return;
}
If (str3 = "")
{
Alert ("Enter the uploaded person! ");
Document. getElementById ("txtUser"). focus ();
Return;
}
Var GetObj = eval ("XinXiFaBuPage ");
Var strFileName = GetObj. GetFileInfo (str1). value;
If (strFileName = "0 ")
{
Alert (strFileName );
Return;
}
Var strResult=GetObj.Insert(str3,strFileName,HtmlEdit.doc ument. body. innerHTML, str2). value;
If (strResult = "1 ")
{
Alert ("File Uploaded successfully! ");
}
Else
{
Alert ("database write failed! ");
}
}
</Script>
<Body>
<Form id = "form1" enctype = "multipart/form-data" runat = "server">
<Div align = "center">
<Table class = "STB" border = "1" width = "470px">
<Tr>
<Td colspan = "2" height = "100px" class = "SC _ B SF_T" align = "center"> water condition information release
</Td>
</Tr>
<Tr>
<Td class = "SF_BL" width = "147px" height = "35px" align = "right">
File name </td>
<Td align = "left">
<Input type = "file" id = "FileUpLoad" runat = "server" style = "width: 295px"/>
</Td>
</Tr>
<Tr>
<Td class = "SF_BL" width = "147px" height = "35px" align = "right">
Title </td>
<Td class = "SF_BR" width = "300px" align = "left" style = "height: 35px">
<Asp: TextBox ID = "txtTitle" runat = "server" Width = "288px"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td class = "SF_BL" width = "147px" height = "35px" align = "right">
Plain text </td>
<Td class = "SF_BR" width = "300px" height = "165px" align = "left">
& nbsp;
<Select language = "javascript" id = "select1" onChange = "format ('fontname', this [this. selectedIndex]. value); "name =" select "onMouseOver =" window. status = 'select the font of the selected text. '; Return true; "onMouseOut =" window. status = ''; return true;" style = "width: 62px">
<! -- <Select name = "selectFont" onChange = "format ('fontname', selectFont. options [selectFont. selectedIndex]. value); selectFont. options [0]. selected = true; "style =" font-family:; font-size: 9pt "onMouseOver =" window. status = 'select the font of the selected text. '; Return true; "onMouseOut =" window. status = ''; return true;"> -->
<Option value = ""> </option>
<Option value = "_ GB2312"> </option>
<Option value = ""> </option>
<Option value = ""> </option>
<Option value = ""> </option>
<Option value = ""> </option>
<OPTION value = "Andale Mono"> Andale Mono </OPTION>
<OPTION value = Arial> Arial </OPTION>
<OPTION value = "Arial Black"> Arial Black </OPTION>
<OPTION value = "Book Antiqua"> Book Antiqua </OPTION>
<OPTION value = "Century Gothic"> Century Gothic </OPTION>
<OPTION value = "Comic Sans MS"> Comic Sans MS </OPTION>
<OPTION value = "Courier New"> Courier New </OPTION>
<OPTION value = Georgia> Georgia </OPTION>
<OPTION value = Impact> Impact </OPTION>
<OPTION value = Tahoma> Tahoma </OPTION>
<OPTION value = "Times New Roman"> Times New Roman </OPTION>
<OPTION value = "Trebuchet MS"> Trebuchet MS </OPTION>
<OPTION value = "Script MT Bold"> Script MT Bold </OPTION>
<OPTION value = stenpencil> stenpencil </OPTION>
<OPTION value = Verdana> Verdana </OPTION>
<OPTION value = "Lucida Console"> Lucida Console </OPTION>
</Select>
<Select language = "javascript" id = "select2" title = "font size" onChange = "format ('fontsize', this [this. selectedIndex]. value); "name =" select "onMouseOver =" window. status = 'select the font size of the selected text. '; Return true; "onMouseOut =" window. status = ''; return true;">
<Option value = "7"> 1
<Option value = "6"> 2
<Option value = "5"> 3
<Option value = "4"> Number 4
<Option value = "3"> 5
<Option value = "2"> 6th
<Option value = "1"> 7 </option>
</Select>
<Iframe class = "HtmlEdit" id = "HtmlEdit" MARGINHEIGHT = "1" MARGINWIDTH = "1" width = "280" height = "135" scrolling = "auto" style =" width: 292px ">
</Iframe>
</Td>
</Tr>
<Tr>
<Td class = "SF_BL" width = "147px" height = "35px" align = "right">
Uploaded By </td>
<Td class = "SF_BR" width = "300px" align = "left" style = "height: 35px">
<Asp: TextBox ID = "txtUser" runat = "server" Width = "284px"> </asp: TextBox>
</Td>
</Tr>
<Tr>
<Td colspan = "2" class = "SC _ B" height = "50px" align = "center">
<Input type = "button" value = "Submit" onclick = "test ()"/>
</Td>
</Tr>
</Table>
<OBJECT id = "dlgHelper" CLASSID = "clsid: 3050f819-98b5-11cf-bb82-00aa00bdce0b" width = "0px" height = "0px"> </OBJECT>
</Div>
</Form>
</Body>
</Html>
II. Background File, FileUpLoad. aspx. cs
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Xml;
Using AjaxPro;
Using System. IO;
Using System. Text;
Using System. Data. SqlClient;
Using BaseCommon. DbCommon;
Using BaseCommon. Exp;
Using BaseCommon. ValueObject;
Public partial class FileUpLoad: BaseCommon. BasePage. BasePage
{
Protected void Page_Load (object sender, EventArgs e)
{
AjaxPro. Utility. RegisterTypeForAjax (typeof (XinXiFaBuPage ));
}
/// <Summary>
/// File Upload
/// </Summary>
/// <Param name = "strFile"> client file path </param>
/// <Returns> </returns>
[AjaxPro. AjaxMethod ()]
Public string GetFileInfo (string strFile)
{
FileStream fs = new FileStream (strFile, FileMode. Open, FileAccess. Read );
BinaryReader binReader = new BinaryReader (fs );
Byte [] buffer = binReader. ReadBytes (int) fs. Length );
BinReader. Close ();
// File Extension
String strExtension = System. IO. Path. GetExtension (strFile );
// File name
String strWithoutExtension = System. IO. Path. GetFileNameWithoutExtension (strFile );
String strFileTemp = strWithoutExtension + strExtension;
WebService web = new WebService ();
Web. bytes = buffer;
Try
{
Web. CreatFile (Server. MapPath ("../") + "Config. config", strFileTemp );
Return strFileTemp;
}
Catch (Exception ex)
{
Return Server. MapPath ("../") + "Config. config ";
}
Return strFileTemp;
}
# Region add data to the database
/// <Summary>
/// Add data to the database
/// </Summary>
/// <Param name = "strUser"> uploaded by </param>
/// <Param name = "strFile"> file name </param>
/// <Param name = "strInfo"> file description </param>
/// <Param name = "strTitle"> file title </param>
[AjaxPro. AjaxMethod ()]
Public string Insert (string strUser, string strFile, string strInfo, string strTitle)
{
Try
{
// Method to start log output
FxLog. StartMethodLog ("XinXiFaBuPage. aspx. cs", "Insert ");
String dtNow = DateTime. Now. ToString ();
// Open the connection
DataBase db = new DataBase ();
Db. OpenConn ();
SqlConnection scon = db. GetConn ();
// Set the command SQL statement
String strSQL = "insert into SL_FILEINFO (USRNAME, TM, FILENAME, TITLE, CONTENT) Values ('" + strUser + "', '" + dtNow + "', '"+ strFile +"', '"+ strTitle +"', '"+ strInfo + "')";
SqlCommand Cmd = new SqlCommand (strSQL, scon );
// Execute
Cmd. ExecuteNonQuery ();
// Close the database
Scon. Close ();
// Method to end log output
FxLog. EndMethodLog ("XinXiFaBuPage. aspx. cs", "Insert ");
Return "1 ";
}
Catch (Exception ex)
{
// Handle error logs
LogVo. LogLevel = SysLog. LogLevel. ERROR;
LogVo. ex = ex;
LogVo. LogMsg = "";
FxLog. LogWrite (logVo );
Throw new UIException (ex );
Return "0 ";
}
}
# Endregion
}