C#文件上傳的簡單實(shí)現(xiàn)
一、分析
本次博客,主要解決文件上傳等一系列問題,將從兩方面來論述,即1G以內(nèi)文件和1G以上文件。
對于上傳1G以內(nèi)的文件,可以采用基本的三種上傳方法:用Web控件FileUpload、html控件HtmlInputFile和用Html元素<input type="file" id="file"/>,通過Request.Files上傳。
對于1G以上的大文件,思路為:
(1)協(xié)議:可采用http協(xié)議或ftp協(xié)議
(2)斷點(diǎn)續(xù)傳
(3)使用插件
(4)非插件形式實(shí)現(xiàn)
二、文件大小屬于[0,1G]范圍
html控件HtmlInputFile實(shí)現(xiàn)上傳:
1、上傳界面
2、前端代碼
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <input type="file" id="file1" runat="server" /> <asp:Button ID="btnUpLoad" runat="server" Text="上傳" OnClick="btnUpLoad_Click" /> <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label> </div> </form> </body> </html>
3、后端代碼
protected void btnUpLoad_Click(object sender, EventArgs e) { //string serverpath = Server.MapPath("~/ImageFile"); if (file1.PostedFile.ContentLength > 0) { if (File.Exists(@"C:\Users\WJM\Desktop\FilesUpLoad\" + file1.PostedFile.FileName)) { Label1.Text = "文件已經(jīng)存在"; } else { file1.PostedFile.SaveAs(@"C:\Users\WJM\Desktop\FilesUpLoad\" + file1.PostedFile.FileName); Label1.Text = "上傳成功!"; } } else { Label1.Text = "上傳失敗"; } }
4、配置文件
<?xml version="1.0" encoding="utf-8"?> <!-- 有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細(xì)信息,請?jiān)L問 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <httpRuntime executionTimeout="36000" delayNotificationTimeout="36000" maxRequestLength="2147483647" targetFramework="4.5"></httpRuntime> <compilation debug="true" targetFramework="4.5" /> <!--<httpRuntime targetFramework="4.5" />--> </system.web> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483648"/> </requestFiltering> </security> </system.webServer> </configuration>
注釋:對于配置文件不太熟悉的朋友,可以參照我的另一篇博客:ASP.NET Web.config
Web控件FileUpload實(shí)現(xiàn)
1、上傳界面
三、文件大小屬于[1G,10G]范圍
注釋:未完,敬請期待。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:c#中的泛型委托詳解
欄 目:C#教程
下一篇:詳解C#中的out和ref
本文標(biāo)題:C#文件上傳的簡單實(shí)現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6026.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已