码迷,mamicode.com
首页 > Web开发 > 详细

文件上传精简

时间:2015-05-12 13:02:15      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  <script src="jquery-1.7.1.min.js"></script>
  <script src="jquery.form.js"></script>
  <script type="text/javascript">
  function upload() {
  $("#form1").ajaxSubmit({
  success: function (str) {
  alert(str);
  },
  error: function (error) { alert(error); },
  url: ‘handler1.ashx‘, /*设置post提交到的页面*/
  type: "post", /*设置表单以post方法提交*/
  dataType: "text" /*设置返回值类型为文本*/
  });
  }
  </script>
</head>
<body>
  <form id="form1" runat="server" enctype="multipart/form-data">
  <input type="file" id="file" name="file" />
  <asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="upload();return false;" />
  </form>
</body>

handler1.ashx

<%@ WebHandler Language="C#" Class="handler1" %>

using System;
using System.Web;

public class handler1 : IHttpHandler {

  public void ProcessRequest (HttpContext context) {
  context.Response.ContentType = "text/plain";
  HttpPostedFile file = context.Request.Files[0];
  String fileName = System.IO.Path.GetFileName(file.FileName);
  file.SaveAs(context.Server.MapPath("~/") + fileName);
  context.Response.Write("OK");
  }

  public bool IsReusable {
  get {
  return false;
  }
  }
}



jquery.form.js地址

文件上传精简

标签:

原文地址:http://www.cnblogs.com/shushan/p/4496918.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!