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ú)刷新上傳文件的話(huà),就要使用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)文章
ASP.Net Core中使用枚舉類(lèi)而不是枚舉的方法
這篇文章主要給大家介紹了關(guān)于ASP.Net Core中使用枚舉類(lèi)而不是枚舉的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.Net Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06.NET?Core項(xiàng)目使用swagger開(kāi)發(fā)組件
這篇文章介紹了.NET?Core項(xiàng)目使用swagger開(kāi)發(fā)組件的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07如何實(shí)現(xiàn)ListView高效分頁(yè)代碼
ListView選擇自動(dòng)分頁(yè)時(shí) 其實(shí)就是添加了一個(gè)DataPager分頁(yè)控件兩者間存在著嵌套關(guān)系《Repeater與ListView》中提到這樣的分頁(yè)并不是高效的 因?yàn)閿?shù)據(jù)源還是返回了所有的數(shù)據(jù) 而非當(dāng)前頁(yè)數(shù)據(jù)2013-02-02ASP.NET抓取網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)方法
這篇文章主要介紹了ASP.NET抓取網(wǎng)頁(yè)內(nèi)容的實(shí)現(xiàn)方法,涉及使用HttpWebRequest及WebResponse抓取網(wǎng)頁(yè)內(nèi)容的技巧,需要的朋友可以參考下2015-02-02在.Net?Framework應(yīng)用中請(qǐng)求HTTP2站點(diǎn)的問(wèn)題解析
隨著各大瀏覽器支持和蘋(píng)果的帶頭效應(yīng),HTTP2的應(yīng)用會(huì)越來(lái)越廣泛,但是規(guī)模龐大的.NET?Framework應(yīng)用卻也不能為了連接HTTP2就升級(jí)到NET?Core平臺(tái)。通過(guò)本文提供的方案,可以最小成本的實(shí)現(xiàn).NET?Framework應(yīng)用成功訪(fǎng)問(wèn)HTTP2站點(diǎn),感興趣的朋友跟隨小編一起看看吧2022-07-07ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection)中篇
這篇文章主要為大家再一次介紹了ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09