[C#] jQuery结合C#上传文件的代码 →→→→→进入此内容的聊天室

来自 , 2019-12-14, 写在 C#, 查看 107 次.
URL http://www.code666.cn/view/894a200a
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head id="Head1" runat="server">
  3.   <script src="jquery-1.7.1.min.js"></script>
  4.   <script src="jquery.form.js"></script>
  5.   <script type="text/javascript">
  6.   function upload() {
  7.   $("#form1").ajaxSubmit({
  8.   success: function (str) {
  9.   alert(str);
  10.   },
  11.   error: function (error) { alert(error); },
  12.   url: 'handler1.ashx', /*设置post提交到的页面*/
  13.   type: "post", /*设置表单以post方法提交*/
  14.   dataType: "text" /*设置返回值类型为文本*/
  15.   });
  16.   }
  17.   </script>
  18. </head>
  19. <body>
  20.   <form id="form1" runat="server" enctype="multipart/form-data">
  21.   <input type="file" id="file" name="file" />
  22.   <asp:Button ID="Button1" runat="server" Text="上传" OnClientClick="upload();return false;" />
  23.   </form>
  24. </body>
  25.  
  26. handler1.ashx
  27.  
  28. <%@ WebHandler Language="C#" Class="handler1" %>
  29.  
  30. using System;
  31. using System.Web;
  32.  
  33. public class handler1 : IHttpHandler {
  34.    
  35.   public void ProcessRequest (HttpContext context) {
  36.   context.Response.ContentType = "text/plain";
  37.   HttpPostedFile file = context.Request.Files[0];
  38.   String fileName = System.IO.Path.GetFileName(file.FileName);
  39.   file.SaveAs(context.Server.MapPath("~/") + fileName);
  40.   context.Response.Write("OK");
  41.   }
  42.  
  43.   public bool IsReusable {
  44.   get {
  45.   return false;
  46.   }
  47.   }
  48. }
  49.  
  50.  
  51. //csharp/6790

回复 "jQuery结合C#上传文件的代码"

这儿你可以回复上面这条便签

captcha