asp.net利用ashx文件實(shí)現(xiàn)文件的上傳功能
原來(lái)以為文件上傳是一個(gè)比較簡(jiǎn)單的功能,結(jié)果搞了一個(gè)晚上才搞定~這里主要介紹兩種方法實(shí)現(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 的摘要說(shuō)明
/// </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;
}
}
}
}
該方法雖然能夠?qū)崿F(xiàn)文件的上傳,但是form表單提交之后整個(gè)頁(yè)面就刷新了,如果要無(wú)刷新上傳文件的話,就要使用ajax了。
方法二:jquery + ajax無(wú)刷上傳
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 的摘要說(shuō)明
/// </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;
}
}
}
}
個(gè)人更推薦方法二,運(yùn)行結(jié)果如下圖所示:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET 上傳文件導(dǎo)入Excel的示例
- asp.net core webapi文件上傳功能的實(shí)現(xiàn)
- ASP.NET Core單文件和多文件上傳并保存到服務(wù)端的方法
- asp.net大文件上傳解決方案實(shí)例代碼
- asp.net上傳Excel文件并讀取數(shù)據(jù)的實(shí)現(xiàn)方法
- ASP.NET MVC實(shí)現(xiàn)批量文件上傳
- ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式)
- 解決asp.net上傳文件超過(guò)了最大請(qǐng)求長(zhǎng)度的問(wèn)題
- ASP.NET MVC HttpPostedFileBase文件上傳的實(shí)例代碼
- ASP.NET 上傳文件到共享文件夾的示例
相關(guān)文章
ashx介紹以及ashx文件與aspx文件之間的區(qū)別
這篇文章主要介紹了ashx以及ashx文件與aspx文件之間的區(qū)別。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-12-12
如何使用pm2守護(hù)你的.NET Core應(yīng)用程序詳解
pm2是nodejs的一個(gè)帶有負(fù)載均衡功能的應(yīng)用進(jìn)程管理器的模塊,下面這篇文章主要給大家介紹了關(guān)于如何使用pm2守護(hù)你的.NET Core應(yīng)用程序的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-10-10
asp.net實(shí)現(xiàn)拒絕頻繁的IP訪問(wèn)的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)拒絕頻繁的IP訪問(wèn)的方法,涉及asp.net針對(duì)訪問(wèn)IP的判斷及配置文件的設(shè)置技巧,需要的朋友可以參考下2016-04-04
ASP.NET生成eurl.axd Http異常錯(cuò)誤的處理方法
在IIS6中同時(shí)啟用了ASP.NET 2.0 和 ASP.NET 4.0 后,網(wǎng)站程序可能會(huì)出現(xiàn)如下錯(cuò)誤:“ System.Web.HttpException: Path ‘//eurl.axd/‘ was not found. ”2011-05-05
Entity Framework Core實(shí)現(xiàn)軟刪除與查詢過(guò)濾器
這篇文章介紹了Entity Framework Core實(shí)現(xiàn)軟刪除與查詢過(guò)濾器的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-02-02
.Net?Core使用Logger實(shí)現(xiàn)log寫(xiě)入本地文件系統(tǒng)
這篇文章介紹了.Net?Core使用Logger實(shí)現(xiàn)log寫(xiě)入本地文件系統(tǒng)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

