日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

ASP.NET實(shí)現(xiàn)文件上傳_實(shí)用技巧

作者:Dust_SongYunfei ? 更新時(shí)間: 2022-09-18 編程語言

本文實(shí)例為大家分享了ASP.NET實(shí)現(xiàn)文件上傳的具體代碼,供大家參考,具體內(nèi)容如下

.NET中C/S和B/S上傳文件不同

B/S中文件上傳和C/S中的文件上傳性質(zhì)完全不一樣

在C/S中文件上傳基本上的原理是:將客戶端計(jì)算機(jī)上的目標(biāo)文件通過Socket網(wǎng)絡(luò)將文件發(fā)送至目標(biāo)服務(wù)器端計(jì)算機(jī),然后將接受到的數(shù)據(jù)轉(zhuǎn)換為原始文件

文件–轉(zhuǎn)成字節(jié)流–發(fā)送到服務(wù)器–將字節(jié)流轉(zhuǎn)成文件–保存

而B/S中文件上傳指的是在客戶端瀏覽器上,將目標(biāo)文件選擇好之后,通過網(wǎng)絡(luò)將文件發(fā)送至目標(biāo)服務(wù)器計(jì)算機(jī),然后將接收到的文件保存在服務(wù)器計(jì)算機(jī)上。

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>
? ? ? ? ? ? 請(qǐng)選擇要上傳的文件:<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);
? ? ? ? ? ? ? ?//獲取配置文件中對(duì)上傳文件大小的限制
? ? ? ? ? ? ? ?double limeitLength = Convert.ToDouble(ConfigurationManager.AppSettings["fileMaxLength"])/1024.0;
? ? ? ? ? ? ? ?if (fileLength>limeitLength)
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?lblMsg.Text = $"上傳文件不能超過{limeitLength}MB";
? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?// 【3】獲取文件名,判斷文件擴(kuò)展名是否符合要求
? ? ? ? ? ? ? ?string fileName = fileup.FileName;
? ? ? ? ? ? ? ?//判斷文件是否exe文件
? ? ? ? ? ? ? ?if (fileName.Substring(fileName.LastIndexOf(".")).ToLower()==".exe")
? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?lblMsg.Text = "不能上傳應(yīng)用程序";
? ? ? ? ? ? ? ? ? ?return;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?//【4】修改文件名稱
? ? ? ? ? ? ? ?//一般情況下,上傳的文件服務(wù)器中保存時(shí)不會(huì)采取原文件名,因?yàn)榭蛻舳擞脩艉荦嫶螅WC每個(gè)用戶端上傳文件不能被覆蓋
? ? ? ? ? ? ? ?fileName = DateTime.Now.ToString("yyyyMMddhhmmssms")+"_"+fileName; //年月日時(shí)分秒毫秒_原文件名 防止文件絕對(duì)覆蓋
? ? ? ? ? ? ? ?//【5】獲取服務(wù)器中存儲(chǔ)文件的路徑
? ? ? ? ? ? ? ?//"~"代表應(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)用程序的詳細(xì)信息,請(qǐng)?jiān)L問
? 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è)置請(qǐng)求的最大字節(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=\&quot;Web\&quot; /optionInfer+"/>
? ? </compilers>
? </system.codedom>
</configuration>

原文鏈接:https://blog.csdn.net/dust__/article/details/105753686

欄目分類
最近更新