一。file.aspx文件代码:
<%@ 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>水情信息发布</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="<head><style type=\"text/css\">body {font-size: 9pt}</style><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"></head><BODY bgcolor=\"#FFFFFF\" MONOSPACE>"
public_description=new Editor
//使IFRAME可以写入值
function document.onreadystatechange(){
HtmlEdit.document.open();
HtmlEdit.document.write(bodyTag);
HtmlEdit.document.close();
HtmlEdit.document.designMode="On";
}
function InitBtn(btn)
{
btn.onmouseover = BtnMouseOver;
btn.onmouseout = BtnMouseOut;
btn.onmousedown = BtnMouseDown;
btn.onmouseup = BtnMouseUp;
btn.YINITIALIZED = true;
return true;
}
//页脚显示
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;
}
//字体颜色
function format(what,opt)
{
if (opt=="removeFormat")
{
what=opt;
opt=null;
}
if (opt==null) HtmlEdit.document.execCommand(what);
else HtmlEdit.document.execCommand(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();
}
//上传
function test()
{
var str=HtmlEdit.document.body.innerHTML;
var str1 = document.getElementById("FileUpLoad").value;
var str2 =document.getElementById("txtTitle").value;
var str3 =document.getElementById("txtUser").value;
if(str1=="")
{
alert("请选择上传文件!");
document.getElementById("FileUpLoad").focus();
return;
}
if(str2=="")
{
alert("请输入文件标题!");
document.getElementById("txtTitle").focus();
return;
}
if(str3=="")
{
alert("请输入上传人!");
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.document.body.innerHTML,str2).value;
if(strResult=="1")
{
alert("文件上传成功!");
}
else
{
alert("数据库写入失败!");
}
}
</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">水情信息发布
</td>
</tr>
<tr>
<td class="SF_BL" width ="147px" height ="35px" align ="right">
文件名</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">
标题</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">
说明文</td>
<td class="SF_BR" width ="300px" height ="165px" align ="left">
<img src="image/fgcolor.gif" align="absmiddle" width="16" height="16" οnclick="foreColor()" title="字体颜色" >
<img src="image/bold.gif" align="absmiddle" alt="粗体" οnclick="format('bold', '')" style="cursor: hand;">
<img src="image/italic.gif" align="absmiddle" alt="斜体" οnclick="format('italic', '')" style="cursor: hand;">
<img src="image/underline.gif" align="absmiddle" alt="下划线" οnclick="format('underline', '')" style="cursor: hand;">
<select language="javascript" id="select1" onChange="format('fontname',this[this.selectedIndex].value);" name="select" onMouseOver="window.status='选择选定文字的字体。';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='选择选定文字的字体。';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=Stencil>Stencil</OPTION>
<OPTION value=Verdana>Verdana</OPTION>
<OPTION value="Lucida Console">Lucida Console</OPTION>
</select>
<select language="javascript" id="select2" title="字号大小" onChange="format('fontsize',this[this.selectedIndex].value);" name="select" onMouseOver="window.status='选择选定文字的字号大小。';return true;" onMouseOut="window.status='';return true;">
<option value="7">一号
<option value="6">二号
<option value="5">三号
<option value="4">四号
<option value="3">五号
<option value="2">六号
<option value="1">七号</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">
上传人</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 ="提交" onclick ="test()" />
</td>
</tr>
</table>
<OBJECT id="dlgHelper" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></OBJECT>
</div>
</form>
</body>
</html>
二。后台文件,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>
/// 文件上传
/// </summary>
/// <param name="strFile">客户端文件路径</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();
//文件扩展名
string strExtension = System.IO.Path.GetExtension(strFile);
//文件名
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 向数据库里添加数据
/// <summary>
/// 向数据库里添加数据
/// </summary>
/// <param name="strUser">上传人</param>
/// <param name="strFile">文件名</param>
/// <param name="strInfo">文件说明</param>
/// <param name="strTitle">文件标题</param>
[AjaxPro.AjaxMethod()]
public string Insert(string strUser, string strFile, string strInfo, string strTitle)
{
try
{
//方法开始log出力
fxLog.StartMethodLog("XinXiFaBuPage.aspx.cs", "Insert");
string dtNow = DateTime.Now.ToString();
//打开连接
DataBase db = new DataBase();
db.OpenConn();
SqlConnection scon = db.GetConn();
//设置命令SQL语句
string strSQL = "INSERT INTO SL_FILEINFO (USRNAME,TM,FILENAME,TITLE,CONTENT) Values('" + strUser + "','" + dtNow + "', '" + strFile + "', '" + strTitle + "','" + strInfo + "')";
SqlCommand Cmd = new SqlCommand(strSQL, scon);
//执行
Cmd.ExecuteNonQuery();
//关闭数据库
scon.Close();
//方法结束log出力
fxLog.EndMethodLog("XinXiFaBuPage.aspx.cs", "Insert");
return "1";
}
catch (Exception ex)
{
//错误 LOG 处理
logVo.LogLevel = SysLog.LogLevel.ERROR;
logVo.ex = ex;
logVo.LogMsg = "";
fxLog.LogWrite(logVo);
throw new UIException(ex);
return "0";
}
}
#endregion
}