手机
当前位置:查字典教程网 >编程开发 >C#教程 >jQuery结合C#实现上传文件的方法
jQuery结合C#实现上传文件的方法
摘要:本文实例讲述了jQuery结合C#实现上传文件的方法。分享给大家供大家参考。具体实现方法如下:functionupload(){$("#fo...

本文实例讲述了jQuery结合C#实现上传文件的方法。分享给大家供大家参考。具体实现方法如下:

<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#" %> 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; } } }

希望本文所述对大家的C#程序设计有所帮助。

【jQuery结合C#实现上传文件的方法】相关文章:

C#对XML文件的各种操作实现方法

C#定位txt指定行的方法小例子

C#实现对AES加密和解密的方法

C#操作目录与文件的方法步骤

解析StreamReader与文件乱码问题的解决方法

深入C# 内存管理以及优化的方法详解

.net后台获取html控件值的2种方法

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

C# 向二进制文件进行读写的操作方法

C#计算代码执行时间的方法

精品推荐
分类导航