欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2022年07月26日 11:25:34   作者:IT-wanghanghang  
這篇文章主要為大家詳細(xì)介紹了ASP.NET實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

1、搭建網(wǎng)站結(jié)構(gòu)

2、編寫網(wǎng)頁文件

創(chuàng)建一個(gè)Web窗體UploadFile和UpFile文件夾,UploadFile包含UploadFile.aspx和UploadFile.aspx.cs兩個(gè)文件,源代碼如下:

[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】獲取文件的大小,判斷是否符合設(shè)置要求
? ? ? ? ? ? ? ? //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】獲取文件名,判斷文件擴(kuò)展名是否符合要求
? ? ? ? ? ? ? ? string fileName = fileup.FileName;
? ? ? ? ? ? ? ? //判斷文件是否是exe文件,則不能上傳
? ? ? ? ? ? ? ? if (fileName.Substring(fileName.LastIndexOf(".")).ToLower()==".exe")
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? lblMsg.Text = "不能上傳應(yīng)用程序";
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //【4】修改文件名稱
? ? ? ? ? ? ? ? //一般情況下,上傳的文件服務(wù)器中保存時(shí)不會(huì)采取原文件名,因?yàn)榭蛻舳擞脩羰欠浅}嫶蟮?,所以要保證每個(gè)客戶端上傳的文件不能被覆蓋
? ? ? ? ? ? ? ? fileName = DateTime.Now.ToString("yyyyMMddhhmmssms") + "_" + fileName;
? ? ? ? ? ? ? ? //【5】獲取服務(wù)器中存儲(chǔ)文件的路徑
? ? ? ? ? ? ? ? //"~"代表應(yīng)用程序的根目錄,從服務(wù)器的根目錄尋找
? ? ? ? ? ? ? ? string path = Server.MapPath("~/UPFile");
? ? ? ? ? ? ? ? //【6】上傳文件
? ? ? ? ? ? ? ? try
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? fileup.SaveAs(path+"/"+fileName);
? ? ? ? ? ? ? ? ? ? lblMsg.Text = "文件上傳成功!";
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? lblMsg.Text = $"文件上傳失?。簕ex.Message}";
? ? ? ? ? ? ? ? }

? ? ? ? ? ? }
? ? ? ? }
? ? }
}

3.在Web.config加入下面代碼:

<appSettings>
? ? <!--配置上傳文件最大字節(jié)數(shù)為30mb:單位kb-->
? ? <add key="fileMaxLength" value="30720"/>
? </appSettings>
? <system.web>
? ? <!--httpRuntime中可以設(shè)置請求的最大字節(jié)數(shù)-->
? ? <httpRuntime targetFramework="4.6.1" maxRequestLength="40960"/>
</system.web>

4、運(yùn)行測試

(1) 點(diǎn)擊選擇文件

(2) 確定好文件

(3)點(diǎn)擊上傳,顯示文件上傳成功。

(4)在程序的目錄下面可以看到剛才上傳的文件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論