jQuery结合C#实现上传文件的方法
本文实例讲述了jQuery结合C#实现上传文件的方法。分享给大家供大家参考。具体实现方法如下:
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headid="Head1"runat="server">
<scriptsrc="jquery-1.7.1.min.js"></script>
<scriptsrc="jquery.form.js"></script>
<scripttype="text/javascript">
functionupload(){
$("#form1").ajaxSubmit({
success:function(str){
alert(str);
},
error:function(error){alert(error);},
url:'handler1.ashx',/*设置post提交到的页面*/
type:"post",/*设置表单以post方法提交*/
dataType:"text"/*设置返回值类型为文本*/
});
}
</script>
</head>
<body>
<formid="form1"runat="server"enctype="multipart/form-data">
<inputtype="file"id="file"name="file"/>
<asp:ButtonID="Button1"runat="server"Text="上传"
OnClientClick="upload();returnfalse;"/>
</form>
</body>
handler1.ashx代码如下:
<%@WebHandlerLanguage="C#"Class="handler1"%>
usingSystem;
usingSystem.Web;
publicclasshandler1:IHttpHandler{
publicvoidProcessRequest(HttpContextcontext){
context.Response.ContentType="text/plain";
HttpPostedFilefile=context.Request.Files[0];
StringfileName=System.IO.Path.GetFileName(file.FileName);
file.SaveAs(context.Server.MapPath("~/")+fileName);
context.Response.Write("OK");
}
publicboolIsReusable{
get{
returnfalse;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语