submit验证
<script type="text/javascript">
function Comit() {
var title = $("#Title").val();
var content = $("#Content").val();
var user = $("#User").val();
if (title.trim() == "") {
alert("标题不可为空!");
return false;
}
if (title.length > 20) {
alert("标题不能超过20字!");
return false;
}
if (content.trim() == "") {
alert("内容不可为空!");
return false;
}
}
</script>
表单提交到一般处理程序fabu.ashx
<form action="fabu.ashx" method="post" enctype="multipart/form-data" οnsubmit="return(Comit())">
<div class="input-group">
<label for="wdname">文章标题:</label>
<input type="text" class="xl" id="Title" name="Title" placeholder="请输入文章标题" />
</div>
<div class="input-group">
<label for="khname">文章内容:</label>
<textarea id="Content" name="Content" rows="8" placeholder="请输入文章内容"></textarea>
</div>
<div class="input-group">
<label for="khname">发布人:</label>
<input type="text" id="User" name="User" placeholder="请输入发布人" />
</div>
<div class="input-group">
<label for="khname">图片地址:</label>
<input id="fileImg" name="fileImg" type="file" />
</div>
<input type="submit" name="btnComit" id="btnComid" value="提交" class="inputbutton" />
</form>
一般处理程序获取流,保存文件
string title = context.Request.Form["Title"];
string content = context.Request.Form["Content"].ToString();
string user = context.Request.Form["User"];
HttpFileCollection httpFiles = context.Request.Files;
HttpPostedFile httpFile = httpFiles["fileImg"];
//获取上传的文件
string filename = "";
string path = "";
string ext = System.IO.Path.GetExtension(httpFile.FileName).ToLower();
if (httpFile != null && httpFile.ContentLength > 0)
{ //获取服务器保存图片的文件夹路径
filename = "touxiang_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ext;
path = "../Upload/image/" + filename;
//获取服务器已保存图片路径
httpFile.SaveAs(context.Server.MapPath(path));
}