jQuery結合C#實現(xiàn)上傳文件的方法
更新時間:2015年04月25日 15:40:12 作者:work24
這篇文章主要介紹了jQuery結合C#實現(xiàn)上傳文件的方法,涉及C#文件上傳的相關技巧,需要的朋友可以參考下
本文實例講述了jQuery結合C#實現(xiàn)上傳文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
<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#程序設計有所幫助。
相關文章
C#先判斷是否存在再創(chuàng)建文件夾或文件與遞歸計算文件夾大小
這篇文章介紹了C#先判斷是否存在再創(chuàng)建文件夾或文件與遞歸計算文件夾大小的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07
用c#獲得當前用戶的Application Data文件夾位置
用c#獲得當前用戶的Application Data文件夾位置...2007-03-03

