文件的上传下载示例

利用.net 提供的Html  input(file)控件,可以简单而且迅速地上传小容量的文件
我经过了测试,把实现的效果,包括创建文件夹,列了出来,供需要的同志查阅

//上传按钮事件
protected void Button1_Click(object sender, EventArgs e)
    {
//首先创立一个保存上传文件的文件夹,当然,如果你不需要创建的话,就可以省略次不步骤了
        string strFilePath = Server.MapPath(Request.ApplicationPath) + "\\MyFilename";
        System.IO.Directory.CreateDirectory(strFilePath); //文件夹创建成功;
        //if (System.IO.Directory.Exists(strFilePath))//文件夹创建成功的检测方法
//==================文件夹创建完毕=============================================
//==================上传程序开始============================================
        try
        {
            string strAllPath = this.File1.PostedFile.FileName;//上传文件的完整原始物理路径
            string strAllName = System.IO.Path.GetFileName(this.File1.PostedFile.FileName);//原文件名(包括格式)
            string strtype = System.IO.Path.GetExtension(this.File1.PostedFile.FileName);//扩展名
            string strName = System.IO.Path.GetFileNameWithoutExtension(this.File1.PostedFile.FileName);//文件名(不包含格式)
            string strSaveName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + strtype;//上传后的文件名称(时间)
            string strSavePath = Server.MapPath(Request.ApplicationPath) + "\\MyFilename\\" + strSaveName;//为文件获取保存路径
            File1.PostedFile.SaveAs(strSavePath);//保存上传的文件到指定的路径
            Response.Write("<script>alert('\"上传文件" + strName + "成功\"')</script>");
        }
        catch (Exception ex)
        {
            this.Page.Controls.Clear();
            Response.Write(ex.ToString());
        }
    }

当然,你需要对上传的东西进行判断的话,只要再加一些东东就可以了,是不是很简单哦???!!!
前台界面就不附加上来了,不过记得设置一个属性 <input id="File1"  runat="server"  type="file" />
呵呵
原文地址:https://www.cnblogs.com/bbxie/p/569627.html