asp.net利用ashx文件實現(xiàn)文件的上傳功能
原來以為文件上傳是一個比較簡單的功能,結果搞了一個晚上才搞定~這里主要介紹兩種方法實現(xiàn)。
方法一:Form表單提交
html代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>上傳文件</title>
<script src="Scripts/jquery-1.11.3.min.js"></script>
</head>
<body>
<form action="UploadHandler.ashx" method="post" enctype="multipart/form-data">
<input id="file_upload" name="file_upload" type="file" />
<input id="btn_upload" type="submit" value="上傳" />
</form>
</body>
</html>
UploadHandler.ashx代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
/// <summary>
/// UploadHandler 的摘要說明
/// </summary>
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
HttpPostedFile file = context.Request.Files["file_upload"];
string filePath = context.Server.MapPath("~/UploadFiles/") + System.IO.Path.GetFileName(file.FileName);
file.SaveAs(filePath);
context.Response.Write("上傳文件成功");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
該方法雖然能夠實現(xiàn)文件的上傳,但是form表單提交之后整個頁面就刷新了,如果要無刷新上傳文件的話,就要使用ajax了。
方法二:jquery + ajax無刷上傳
html代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>上傳文件</title>
<script src="Scripts/jquery-1.11.3.min.js"></script>
</head>
<body>
<input id="file_upload" name="file_upload" type="file" />
<input id="btn_upload" type="button" value="上傳" />
<script>
$(document).ready(function ()
{
$('#btn_upload').bind('click', function ()
{
var formData = new FormData();
formData.append('upload_file', $('#file_upload')[0].files[0]);
$.ajax({
url: 'UploadHandler.ashx',
type: 'post',
data: formData,
contentType: false,
processData: false,
success: function (msg)
{
if (msg == "Yes")
{
alert('文件上傳成功');
}
else
{
alert('文件上傳失敗');
}
}
})
});
});
</script>
</body>
</html>
UploadHandler.ashx代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
/// <summary>
/// UploadHandler 的摘要說明
/// </summary>
public class UploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = context.Request.Files["upload_file"];
string filePath = context.Server.MapPath("~/UploadFiles/") + System.IO.Path.GetFileName(file.FileName);
file.SaveAs(filePath);
context.Response.Write("Yes");
}
else
{
context.Response.Write("No");
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
個人更推薦方法二,運行結果如下圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
ashx介紹以及ashx文件與aspx文件之間的區(qū)別
這篇文章主要介紹了ashx以及ashx文件與aspx文件之間的區(qū)別。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12
ASP.NET生成eurl.axd Http異常錯誤的處理方法
在IIS6中同時啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會出現(xiàn)如下錯誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-05-05
Entity Framework Core實現(xiàn)軟刪除與查詢過濾器
這篇文章介紹了Entity Framework Core實現(xiàn)軟刪除與查詢過濾器的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
.Net?Core使用Logger實現(xiàn)log寫入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實現(xiàn)log寫入本地文件系統(tǒng)的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

