網站首頁 編程語言 正文
本文實例為大家分享了ASP.NET實現文件上傳的具體代碼,供大家參考,具體內容如下
.NET中C/S和B/S上傳文件不同
B/S中文件上傳和C/S中的文件上傳性質完全不一樣
在C/S中文件上傳基本上的原理是:將客戶端計算機上的目標文件通過Socket網絡將文件發送至目標服務器端計算機,然后將接受到的數據轉換為原始文件
文件–轉成字節流–發送到服務器–將字節流轉成文件–保存
而B/S中文件上傳指的是在客戶端瀏覽器上,將目標文件選擇好之后,通過網絡將文件發送至目標服務器計算機,然后將接收到的文件保存在服務器計算機上。
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> ? ? <!--配置上傳文件最大的字節數:kb單位--><!--30MB--> ? ? <add key="fileMaxLength" value="30720"/> ? </appSettings> ? <system.web> ? ? <compilation debug="true" targetFramework="4.6.1"/> ? ? <!--httpRuntime中可以設置請求的最大字節數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>
原文鏈接:https://blog.csdn.net/dust__/article/details/105753686
相關推薦
- 2022-01-08 出現WARN org.springframework.web.servlet.PageNotFoun
- 2022-06-15 C#實現冒泡排序和插入排序算法_C#教程
- 2023-03-18 pandas檢查和填充缺失值的N種方法總結_python
- 2022-06-02 Android?實例代碼帶你掌握FrameLayout_Android
- 2023-02-15 PyQt5+PyQt5Designer的安裝步驟_python
- 2022-07-11 spring boot中動態代理導致自定義注解掃描失敗以及解決辦法
- 2022-12-07 React?Context?變遷及背后實現原理詳解_React
- 2022-09-07 Python中的if判斷語句中包含or問題_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支