網站首頁 編程語言 正文
本文實例為大家分享了ASP.NET實現文件上傳功能的具體代碼,供大家參考,具體內容如下
1、搭建網站結構
2、編寫網頁文件
創建一個Web窗體UploadFile和UpFile文件夾,UploadFile包含UploadFile.aspx和UploadFile.aspx.cs兩個文件,源代碼如下:
[UploadFile.aspx]
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFile.aspx.cs" Inherits="WebForrmDemo.UploadFile" %>
<!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"/>
? ? ? ? ? ? <br />
? ? ? ? ? ? <asp:Literal ID="lblMsg" runat="server"></asp:Literal>
? ? ? ? </div>
? ? </form>
</body>
</html>
[UploadFile.aspx.cs]
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 WebForrmDemo
{
? ? public partial class UploadFile : 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】獲取文件的大小,判斷是否符合設置要求
? ? ? ? ? ? ? ? //1mb=1024kb
? ? ? ? ? ? ? ? //1kb=1024byte
? ? ? ? ? ? ? ? double fileLength=fileup.FileContent.Length / (1024.0 * 1024.0);
? ? ? ? ? ? ? ? //獲取配置文件中上傳文件大小的限制
? ? ? ? ? ? ? ? double limitLength = Convert.ToDouble(ConfigurationManager.AppSettings["fileMaxLength"])/1024.0;
? ? ? ? ? ? ? ? if (fileLength>limitLength)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? lblMsg.Text = $"上傳文件不能超過{limitLength}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】獲取服務器中存儲文件的路徑
? ? ? ? ? ? ? ? //"~"代表應用程序的根目錄,從服務器的根目錄尋找
? ? ? ? ? ? ? ? string path = Server.MapPath("~/UPFile");
? ? ? ? ? ? ? ? //【6】上傳文件
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? fileup.SaveAs(path+"/"+fileName);
? ? ? ? ? ? ? ? ? ? lblMsg.Text = "文件上傳成功!";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? lblMsg.Text = $"文件上傳失敗:{ex.Message}";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
3.在Web.config加入下面代碼:
<appSettings> ? ? <!--配置上傳文件最大字節數為30mb:單位kb--> ? ? <add key="fileMaxLength" value="30720"/> ? </appSettings> ? <system.web> ? ? <!--httpRuntime中可以設置請求的最大字節數--> ? ? <httpRuntime targetFramework="4.6.1" maxRequestLength="40960"/> </system.web>
4、運行測試
(1) 點擊選擇文件
(2) 確定好文件
(3)點擊上傳,顯示文件上傳成功。
(4)在程序的目錄下面可以看到剛才上傳的文件
原文鏈接:https://wanghanghang.blog.csdn.net/article/details/105727464
相關推薦
- 2022-05-25 Entity?Framework?Core對Web項目生成數據庫表_實用技巧
- 2022-02-24 Promise.all() 函數的常用場景
- 2023-12-11 Spring中的事務管理
- 2022-10-06 QT獲取顯示當前時間和日期的方法(用QTime,QDate和QDateTime)_C 語言
- 2022-08-05 Python時間操作之pytz模塊使用詳解_python
- 2023-06-04 pycharm中下載的包但是import還是無法使用/報紅的解決方法_python
- 2023-01-14 C++之eigen安裝與測試方式_C 語言
- 2023-10-26 ElementUI日期轉為“yyyy-MM-dd“格式
- 最近更新
-
- 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同步修改后的遠程分支