ASP.NET——使用FileUpLoad服务器控件实现文件上传

ASP.NET——使用FileUpLoad服务器控件实现文件上传

文件上传思路分析

在这里插入图片描述

需要在Web.config配置文件中配置请求最大字节数

在这里插入图片描述

选择服务器控件

在这里插入图片描述

前端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo09_UpLoad.aspx.cs" Inherits="WebApp.Demo09_UpLoad" %>

<!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="ful" runat="server" />
        &nbsp;&nbsp;
        <asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click" />
        <br />
        <br />
        <asp:Literal ID="ltaMsg" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

后台按钮点击事件代码:

protected void btnUpload_Click(object sender, EventArgs e)
{
    //【1】判断文件是否存在
    if (!this.ful.HasFile) return;
    //【2】获取文件大小,判断是否符合设置要求(变成MB)
    double fileLength = this.ful.FileContent.Length / (1024.0 * 1024.0);
    //获取配置文件中上传文件大小的限制
    double limitedLength = Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["PhysicsObjectLength"]);
    limitedLength = limitedLength / 1024.0;//转换成MB单位
    //判断实际文件大小是否符合要求
    if (fileLength > limitedLength)
    {
        //  this.ltaMsg.Text = "上传文件大小不能超过" + limitedLength + "MB";
        this.ltaMsg.Text = "<script type='text/javascript'>alert('上传文件最大不能超过" + limitedLength + "M')</script>";
        return;
    }
    //【3】获取文件名,判断文件扩展是否符合要求
    string fileName = this.ful.FileName;
    //判断文件名是否是exe文件
    if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower() == "exe")
    {
        this.ltaMsg.Text = "<script type='text/javascript'>alert('上传文件不能是exe文件')</script>";
        return;
    }
    //修改文件名  
    fileName = DateTime.Now.ToString("yyyyMMddhhssms") + "_" + fileName;

    //【4】获取服务器文件夹路径
    string path = Server.MapPath("~/UploadFiles");
    //【5】上传文件
    try
    {
        this.ful.SaveAs(path + "/" + fileName);
        this.ltaMsg.Text = "<script type='text/javascript'>alert('文件上传成功!')</script>";
    }
    catch (Exception ex)
    {
        this.ltaMsg.Text = "<script type='text/javascript'>alert('文件上传失败!" + ex.Message + "')</script>";
    }
}

Web.config配置文件内容

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>

  <appSettings>
    <!--配置上传文件最大字节数:单位KB-->
    <add key="PhysicsObjectLength" value="30720"/>
  </appSettings>
    <system.web>

      <!--设置请求的最大字节数(默认是4096,单位:KB)-->
      <httpRuntime maxRequestLength="40960"></httpRuntime>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老陈聊架构

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值