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

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控件。

上一篇:如何在CRM WebClient UI里打开外部链接


下一篇:真相 | Facebook AI系统并未“失控”,研究员已修正bug