文件上传后台程序实践与测试(L15/03/04)

 

1、前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo09_Upload.aspx.cs" Inherits="Demo0303.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" />
        <br />
        <br/>
        <asp:Button ID="btnUpload" runat="server" Text="开始上传" OnClick="btnUpload_Click" />
        <br/>
        <br/>
        <asp:Literal ID="ltaMsg" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

2、后台程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo0303
{
    public partial class Demo09_Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        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);
            //[3]获取配置文件中上传文件大小的限制
            double limitedLength = Convert.ToDouble(System.Configuration.ConfigurationManager.AppSettings["PhysicsObjectLength"]);
            limitedLength = limitedLength / 1024.0;//转换成MB单位
            //判断实际文件大小是否符合要求
            if (fileLength > limitedLength)
            {
                //简单提示
                //this.ltaMsg.Text = "删除上传文件大小不能超过" + limitedLength + "MB";
                //----------------------------
                //用javascript脚本提示(以后常用)
                this.ltaMsg.Text = "<script type='text/javascript'>alert('上传文件最大不能超过" +limitedLength + "MB' )</script>";
                return;
            }
            //[4]获取文件名,判断文件扩展名是否符合要求
            string fileName = this.ful.FileName;
            //[5]判断文件名是否是exe文件
            if (fileName.Substring(fileName.LastIndexOf(".")).ToLower() == ".exe") 
            {
                this.ltaMsg.Text = "<script type='text/javascript'>alert('上传文件不能是exe文件')</script>";
                return;
            }
            //[6]修改文件名:例上传abc.doc,201805131744+秒+毫秒_文件名
            fileName = DateTime.Now.ToString("yyyyMMddhhssms") + "_" + fileName;

            //[7]获取服务器文件夹路径http://www.abcde.com/UploadFiles
            string path = Server.MapPath("~/UpLoadFiles");

            //[8]上传文件
            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>";
            }
        }
    }
}

3、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"/>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>

</configuration>

 

posted @ 2018-05-13 18:14  爱踢两键  阅读(159)  评论(0编辑  收藏  举报