大文件上传代码,根据下拉列表框选中的市县镇名保存文件到指定目录下,并将文件相关信息写进数据库

简介: using System;using System.Data;using System.Configuration;using System.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using Brettle.Web.NeatUpload;

public partial class Admin_UpdateUserPwd : System.Web.UI.Page
{
    DM dm = new DM();
    protected void Page_Load(object sender, EventArgs e)
    {
        //判断用户是否是管理员登录...
        if (Session["AdminUserName"] == null)
        {
            //返回到上一个页面
            Response.Write("<script>history.back()</script>");
            //跳转到登录页面
            Server.Transfer("~/Admin/AdminLogin.aspx");
        }
        if (!IsPostBack)
        {
            //页面加载时,绑定市县信息
            DataSet ds = dm.GetData("select code,name from Unit where code like '____00000000'order by code");
            this.ddlCity.DataSource = ds;
            this.ddlCity.DataTextField = "name";
            this.ddlCity.DataValueField = "code";
            this.ddlCity.DataBind();
            if (this.ddlCity.Items.Count > 0) this.ddlCounty.SelectedIndex = 0;
            set_ddl_coun();
            if (ddlCounty.SelectedIndex == 0)
            {
                ddlTown.Visible = false;
            }
            else
            {
                if (this.ddlCounty.Items.Count > 0) this.ddlTown.SelectedIndex = 0;
                set_ddl_town();
            }
        }
    }

    //市级下拉引起县级级联提取出来的公共方法
    void set_ddl_coun()
    {
        DM dm = new DM();
        string citycode = ddlCity.SelectedValue;
        if (citycode == "340000000000")
        {
            this.ddlCounty.Visible = false;
            this.ddlCounty.Items.Clear();
        }
        else
        {
            this.ddlCounty.Visible = true;
            this.ddlCounty.Items.Clear();
        }
        DataSet ds = dm.GetData("select code,name from Unit where code like '" + citycode.Substring(0, 4) + "__000000'order by code");
        this.ddlCounty.DataSource = ds;
        this.ddlCounty.DataTextField = "name";
        this.ddlCounty.DataValueField = "code";
        this.ddlCounty.DataBind();
        if (this.ddlCounty.Items.Count > 0) this.ddlCounty.SelectedIndex = 0;
    }

    //市级选择引起县级联动
    protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        set_ddl_coun();
    }

    //大容量文件上传功能
    protected void BtnUP_Click(object sender, EventArgs e)
    {
        string cityName = this.ddlCity.SelectedItem.Text.ToString();
        string countyName = this.ddlCounty.SelectedItem.Text.ToString();
        string townName = this.ddlTown.SelectedItem.Text.ToString();
        //将文件上传到UpLoads/市名/县名/镇名文件夹下去
        FileUpLoad(AttachFile, cityName+"//"+countyName+"//"+townName);
    }

    /// <summary>
    /// 上传文件功能
    /// </summary>
    /// <param name="fulFile">上传控件 </param>
    /// <param name="DirectionName">文件所放的父文件夹 </param>
    /// <returns> </returns>
    public void FileUpLoad(InputFile fulFile,string DirectionName)
    {
        string FileName;
        string FileSavePath;
        string newFileName;
        FileSavePath = Server.MapPath("UpLoads/");  //获取文件存放的根路径

        if (fulFile.HasFile)    //判断是否选择了要上传的文件
        {
            FileName = this.AttachFile.FileName;//获取上传文件的文件名,包括后缀
            //FileInfo file = new FileInfo(FileName);
            string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
            newFileName = DateTime.Now.ToString("yyyyMMddhhmm") + ExtenName;
            float FileSize = (float)System.Math.Round((float)fulFile.ContentLength / 1024000, 1); //获取文件大小并保留小数点后一位,单位是M
            if (!File.Exists(FileSavePath + "//" + DirectionName))
            {
                Directory.CreateDirectory(FileSavePath + "//" + DirectionName);  //如果文件存放的文件夹不存在,则创建该文件夹
            }
            FileSavePath += DirectionName + "
//"+newFileName;
            //判断所上传的文件文件是否存在
            if (!File.Exists(FileSavePath)) 
            {
                try
                {
                    fulFile.MoveTo(FileSavePath, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
                }
                catch (Exception Error)
                {
                    HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
                    return;
                }
            }
            else
            {
                //在该处操作基本与上相同,只是不用创建文件夹了           
            }
        }
        else
        {
            HttpContext.Current.Response.Write(" <script>alert('请选择要上传的文件') </script>");
            return;
        }
        HttpContext.Current.Response.Write(" <script>alert('文件上传成功!') </script>");
        //文件上传成功以后,将信息保存到数据库中去,包含的信息由文件名,文件所属县,文件上传时间。
        string cityName = this.ddlCity.SelectedItem.Text.ToString();  //文件所属市名称
        string countyName = this.ddlCounty.SelectedItem.Text.ToString();  //文件所属县名称
        string townName = this.ddlTown.SelectedItem.Text.ToString();  //文件所属镇名称
        string filePath = FileSavePath;  //文件路径名称
        string fileName = newFileName;
        string strSQL = "insert into UploadInfo values('"+filePath+"','"+fileName+"','"+DateTime.Now.ToString()+"','"+cityName+"','"+countyName+"','"+townName+"')";
        int ret = dm.SetData(strSQL);
        if (ret > 0)
        {
            //说明上传文件的同时向数据库中写入了文件的相关信息
            //Response.Write("<script>alert('文件信息写入数据库成功!')</script>");
        }
    }

    //县级下拉引起镇级级联提取出来的公共方法
    void set_ddl_town()
    {
        DM dm = new DM();
        string countycode = ddlCounty.SelectedValue;
        if (countycode == ddlCounty.SelectedValue.ToString().Substring(0, 4) + "__000000")
        {
            if (ddlCounty.SelectedIndex == 0)
            {
                this.ddlTown.Visible = false;
                this.ddlTown.Items.Clear();
            }
            else
            {
                this.ddlTown.Visible = true;
                this.ddlTown.Items.Clear();
            }
        }
        else
        {
            this.ddlTown.Visible = true;
            this.ddlTown.Items.Clear();
        }

        DataSet ds = dm.GetData("select code,name from Unit where code like '" + countycode.Substring(0, 6) + "___000'order by code");
        this.ddlTown.DataSource = ds;
        this.ddlTown.DataTextField = "name";
        this.ddlTown.DataValueField = "name";
        this.ddlTown.DataBind();
        if (this.ddlTown.Items.Count > 0) this.ddlTown.SelectedIndex = 0;
    }

    //县级选择引起镇级联动
    protected void ddlCounty_SelectedIndexChanged(object sender, EventArgs e)
    {
        set_ddl_town();
    }
}

 

上面使用了一个免费的大文件上传空间,NetUpload控件。

相关文章
|
24天前
|
监控 关系型数据库 数据库
OceanBase数据库常见问题之文件存在但是数据库提示文件不存在如何解决
OceanBase 是一款由阿里巴巴集团研发的企业级分布式关系型数据库,它具有高可用、高性能、可水平扩展等特点。以下是OceanBase 数据库使用过程中可能遇到的一些常见问题及其解答的汇总,以帮助用户更好地理解和使用这款数据库产品。
|
1月前
|
数据采集 Java 关系型数据库
Java代码高效连接数据库
Java代码高效连接数据库
18 2
|
29天前
|
SQL Java 数据库连接
从来没想到我们会扒拉nohup文件去找我们想要的数据,然后往数据库中添加。。。...
从来没想到我们会扒拉nohup文件去找我们想要的数据,然后往数据库中添加。。。...
17 0
|
2天前
|
存储 关系型数据库 MySQL
如何处理爬取到的数据,例如存储到数据库或文件中?
处理爬取的数据,可存储为txt、csv(适合表格数据)或json(适合结构化数据)文件。若需存储大量数据并执行复杂查询,可选择关系型(如MySQL)或非关系型(如MongoDB)数据库。以MySQL为例,需安装数据库和Python的pymysql库,创建数据库和表,然后编写Python代码进行数据操作。选择存储方式应考虑数据类型、数量及后续处理需求。
8 1
|
10天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
23天前
|
SQL 关系型数据库 MySQL
Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中
Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中
10 0
|
29天前
|
SQL Java 数据库连接
springboot解析txt文件顺便加到数据库中(nohup文件)
springboot解析txt文件顺便加到数据库中(nohup文件)
108 1
|
29天前
|
SQL Oracle 关系型数据库
干货!sqlserver数据库所有知识点总结整理,含代码(挺全的)
干货!sqlserver数据库所有知识点总结整理,含代码(挺全的)
11 0
|
16天前
|
SQL 数据可视化 关系型数据库
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
轻松入门MySQL:深入探究MySQL的ER模型,数据库设计的利器与挑战(22)
|
16天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)