ASP.NET實現(xiàn)文件上傳
本文實例為大家分享了ASP.NET實現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下
.NET中C/S和B/S上傳文件不同
B/S中文件上傳和C/S中的文件上傳性質(zhì)完全不一樣
在C/S中文件上傳基本上的原理是:將客戶端計算機上的目標文件通過Socket網(wǎng)絡(luò)將文件發(fā)送至目標服務(wù)器端計算機,然后將接受到的數(shù)據(jù)轉(zhuǎn)換為原始文件
文件–轉(zhuǎn)成字節(jié)流–發(fā)送到服務(wù)器–將字節(jié)流轉(zhuǎn)成文件–保存
而B/S中文件上傳指的是在客戶端瀏覽器上,將目標文件選擇好之后,通過網(wǎng)絡(luò)將文件發(fā)送至目標服務(wù)器計算機,然后將接收到的文件保存在服務(wù)器計算機上。
B/S上傳文件
頁面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="UpLoadFileDemo.WebForm1" %> <!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> ? ? ? ? ? ? 請選擇要上傳的文件:<asp:FileUpload ID="fileup" runat="server" /> ? ? ? ? ? ? <asp:Button ID="btnUpload" runat="server" Text="開始上傳" OnClick="btnUpload_Click" /> ? ? ? ? ? ? <asp:Literal ID="lblMsg" runat="server"></asp:Literal> ? ? ? ? </div> ? ? </form> </body> </html>
事件:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; namespace UpLoadFileDemo { ? ?public partial class WebForm1 : System.Web.UI.Page ? ?{ ? ? ? ?protected void Page_Load(object sender, EventArgs e) ? ? ? ?{ ? ? ? ?} ? ? ? ?protected void btnUpload_Click(object sender, EventArgs e) ? ? ? ?{ ? ? ? ? ? ?//【1】判斷文件是否存在 ? ? ? ? ? ?if (fileup.HasFile) ? ? ? ? ? ?{ ? ? ? ? ? ? ? ?//【2】獲取文件的大小,判斷是否符合設(shè)置要求 ? ? ? ? ? ? ? ?double fileLength = fileup.FileContent.Length / (1024.0*1024.0); ? ? ? ? ? ? ? ?//獲取配置文件中對上傳文件大小的限制 ? ? ? ? ? ? ? ?double limeitLength = Convert.ToDouble(ConfigurationManager.AppSettings["fileMaxLength"])/1024.0; ? ? ? ? ? ? ? ?if (fileLength>limeitLength) ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?lblMsg.Text = $"上傳文件不能超過{limeitLength}MB"; ? ? ? ? ? ? ? ? ? ?return; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?// 【3】獲取文件名,判斷文件擴展名是否符合要求 ? ? ? ? ? ? ? ?string fileName = fileup.FileName; ? ? ? ? ? ? ? ?//判斷文件是否exe文件 ? ? ? ? ? ? ? ?if (fileName.Substring(fileName.LastIndexOf(".")).ToLower()==".exe") ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?lblMsg.Text = "不能上傳應(yīng)用程序"; ? ? ? ? ? ? ? ? ? ?return; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?//【4】修改文件名稱 ? ? ? ? ? ? ? ?//一般情況下,上傳的文件服務(wù)器中保存時不會采取原文件名,因為客戶端用戶很龐大,要保證每個用戶端上傳文件不能被覆蓋 ? ? ? ? ? ? ? ?fileName = DateTime.Now.ToString("yyyyMMddhhmmssms")+"_"+fileName; //年月日時分秒毫秒_原文件名 防止文件絕對覆蓋 ? ? ? ? ? ? ? ?//【5】獲取服務(wù)器中存儲文件的路徑 ? ? ? ? ? ? ? ?//"~"代表應(yīng)用程序的根目錄,從服務(wù)器的根目錄找 ? ? ? ? ? ? ? ? ?//"~" Shift鍵+左上角的"`"鍵 ? ? ? ? ? ? ? ?string path = Server.MapPath("~/UpFile"); ? ? ? ? ? ? ? ?//【6】上傳文件 ? ? ? ? ? ? ? ?try ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?fileup.SaveAs(path+"/"+fileName);//參數(shù):要上傳到的文件完整路徑,路徑+"/"+文件名 ? ? ? ? ? ? ? ? ? ?lblMsg.Text = "文件上傳成功"; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?catch (Exception ex) ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?lblMsg.Text = $"文件上傳失敗{ex.Message}"; ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ?} ? ?} }
配置文件:
<?xml version="1.0" encoding="utf-8"?> <!-- ? 有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細信息,請訪問 ? https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> ? <appSettings> ? ? <!--配置上傳文件最大的字節(jié)數(shù):kb單位--><!--30MB--> ? ? <add key="fileMaxLength" value="30720"/> ? </appSettings> ? <system.web> ? ? <compilation debug="true" targetFramework="4.6.1"/> ? ? <!--httpRuntime中可以設(shè)置請求的最大字節(jié)數(shù)maxRequestLength--> ? ? <httpRuntime targetFramework="4.6.1" maxRequestLength="40960"/> ? </system.web> ? <system.codedom> ? ? <compilers> ? ? ? <compiler language="c#;cs;csharp" extension=".cs" ? ? ? ? type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" ? ? ? ? warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/> ? ? ? <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" ? ? ? ? type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" ? ? ? ? warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/> ? ? </compilers> ? </system.codedom> </configuration>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET動態(tài)設(shè)置頁面標題的方法詳解
這篇文章介紹了ASP.NET動態(tài)設(shè)置頁面標題的方法詳解,有需要的朋友可以參考一下2013-07-07詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由
本篇文章主要介紹了詳解ASP.NET Core MVC 源碼學(xué)習(xí):Routing 路由 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03visual Studio 2017創(chuàng)建簡單控制臺程序
這篇文章主要為大家詳細介紹了visual Studio 2017創(chuàng)建簡單控制臺程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Asp.Net Couchbase Memcached圖文安裝調(diào)用開發(fā)
本文主要是是如何安裝CouchBase服務(wù)端,以及客戶端如何進行調(diào)用。圖文詳解,大家參考吧2013-11-11asp.net 分頁顯示數(shù)據(jù)表的數(shù)據(jù)的代碼
asp.net顯示第一頁、上一頁、下一頁和最后一頁的分頁顯示數(shù)據(jù)表的數(shù)據(jù)2010-03-03ASP.NET 根據(jù)漢字獲取漢字拼音的首字母(含多音字)
本文分享了一個函數(shù),這個函數(shù)可以根據(jù)漢字的字符串獲取其拼音的首字母,以便我們在實際開發(fā)中使用。2016-04-04