jQuery聯合C#完成上傳文件的辦法。本站提示廣大學習愛好者:(jQuery聯合C#完成上傳文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是jQuery聯合C#完成上傳文件的辦法正文
本文實例講述了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#法式設計有所贊助。