Web .net form表单 上传图片

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));   

 }

转载于:https://www.cnblogs.com/qq867016029/p/5337664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值