当前位置: 移动技术网 > IT编程>开发语言>c# > jQuery结合C#实现上传文件的方法

jQuery结合C#实现上传文件的方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了jquery结合c#实现上传文件的方法。分享给大家供大家参考。具体实现方法如下: <html xmlns="http://www.w3.org

本文实例讲述了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#" 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;
 }
 }
}

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网