ASP.NET實現文件上傳
本文實例為大家分享了ASP.NET實現文件上傳的具體代碼,供大家參考,具體內容如下
.NET中C/S和B/S上傳文件不同
B/S中文件上傳和C/S中的文件上傳性質完全不一樣
在C/S中文件上傳基本上的原理是:將客戶端計算機上的目標文件通過Socket網絡將文件發(fā)送至目標服務器端計算機,然后將接受到的數據轉換為原始文件
文件–轉成字節(jié)流–發(fā)送到服務器–將字節(jié)流轉成文件–保存
而B/S中文件上傳指的是在客戶端瀏覽器上,將目標文件選擇好之后,通過網絡將文件發(fā)送至目標服務器計算機,然后將接收到的文件保存在服務器計算機上。
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】獲取文件的大小,判斷是否符合設置要求 ? ? ? ? ? ? ? ?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 = "不能上傳應用程序"; ? ? ? ? ? ? ? ? ? ?return; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?//【4】修改文件名稱 ? ? ? ? ? ? ? ?//一般情況下,上傳的文件服務器中保存時不會采取原文件名,因為客戶端用戶很龐大,要保證每個用戶端上傳文件不能被覆蓋 ? ? ? ? ? ? ? ?fileName = DateTime.Now.ToString("yyyyMMddhhmmssms")+"_"+fileName; //年月日時分秒毫秒_原文件名 防止文件絕對覆蓋 ? ? ? ? ? ? ? ?//【5】獲取服務器中存儲文件的路徑 ? ? ? ? ? ? ? ?//"~"代表應用程序的根目錄,從服務器的根目錄找 ? ? ? ? ? ? ? ? ?//"~" Shift鍵+左上角的"`"鍵 ? ? ? ? ? ? ? ?string path = Server.MapPath("~/UpFile"); ? ? ? ? ? ? ? ?//【6】上傳文件 ? ? ? ? ? ? ? ?try ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?fileup.SaveAs(path+"/"+fileName);//參數:要上傳到的文件完整路徑,路徑+"/"+文件名 ? ? ? ? ? ? ? ? ? ?lblMsg.Text = "文件上傳成功"; ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ?catch (Exception ex) ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ? ? ? ?lblMsg.Text = $"文件上傳失敗{ex.Message}"; ? ? ? ? ? ? ? ?} ? ? ? ? ? ?} ? ? ? ?} ? ?} }
配置文件:
<?xml version="1.0" encoding="utf-8"?> <!-- ? 有關如何配置 ASP.NET 應用程序的詳細信息,請訪問 ? https://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> ? <appSettings> ? ? <!--配置上傳文件最大的字節(jié)數:kb單位--><!--30MB--> ? ? <add key="fileMaxLength" value="30720"/> ? </appSettings> ? <system.web> ? ? <compilation debug="true" targetFramework="4.6.1"/> ? ? <!--httpRuntime中可以設置請求的最大字節(jié)數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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解ASP.NET Core MVC 源碼學習:Routing 路由
本篇文章主要介紹了詳解ASP.NET Core MVC 源碼學習:Routing 路由 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03visual Studio 2017創(chuàng)建簡單控制臺程序
這篇文章主要為大家詳細介紹了visual Studio 2017創(chuàng)建簡單控制臺程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11Asp.Net Couchbase Memcached圖文安裝調用開發(fā)
本文主要是是如何安裝CouchBase服務端,以及客戶端如何進行調用。圖文詳解,大家參考吧2013-11-11