首页 ┆ 网站地图 ┆ 在线留言 ┆ 游戏资讯 ┆ 资源下载 
设为首页
加入收藏
联系我们
热门关键字: .net应用  操作系统  Dreamweaver  WinRAR  网络推广
高级搜索
您当前的位置: 主页>NET专区>ASP.NET>asp.net(C#)上传下载及文件管理代码实例
asp.net(C#)上传下载及文件管理代码实例
来源: 发布时间:2007-12-25 发布人: 浏览: 人次   字体: [ ]  
asp.net(C#)上传下载及文件管理代码实例如下:

using System;
using System.Data;
using System.Configuration;
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.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //初始化文件夹信息
            InitFolderInfo();
            //初始化上传限制信息
            InitUploadLimit();
            //初始化列表框控件文件列表信息
            InitFileList();
        }
    }
    #region 初始化文件夹信息
    private void InitFolderInfo()
    {
        //从config中读取文件上传路径
        string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
        //如果上传文件夹不存在,则根据config创建一个
        if(!Directory.Exists(Server.MapPath(strFileUpladPath)))
        {
            Directory.CreateDirectory(Server.MapPath(strFileUpladPath));
        }
        //将虚拟路径转换为物理路径
        string strFilePath = Server.MapPath(strFileUpladPath);
        //从config里读取文件夹容量限制
        double iFolderSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FolderSizeLimit"]);
        //声明文件夹已经使用的容量
        double iFolderCurrentSize = 0;
        //获取文件夹中的所有文件
        FileInfo[] arrFiles = new DirectoryInfo(strFilePath).GetFiles();
        //循环文件获已经使用的容量
        foreach (FileInfo fi in arrFiles)
        {
            iFolderCurrentSize += Convert.ToInt32(fi.Length / 1024);
        }
        #region 第二种获得文件夹使用大小的方法
        //DirectoryInfo dir = new DirectoryInfo(strFilePath);
        //foreach (FileSystemInfo fi in dir.GetFileSystemInfos())
        //{
        //    FileInfo finf = new FileInfo(fi.FullName);
        //    iFolderCurrentSize += Convert.ToInt32(finf.Length / 1024);
        //}
        #endregion
        //把文件夹容量和以用文件夹容量赋值给标签
        lbl_FolderInfo.Text = string.Format("文件夹容量限制:{0}M,已用容量:{1}KB", iFolderSizeLimit / 1024, iFolderCurrentSize);
    }
    #endregion
    #region 初始化上传限制信息
    private void InitUploadLimit()
    {
        //从config中读取上传文件夹类型限制并根据逗号分割成字符串数组
        string[] arrFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString().Split(',');
        //从config中读取上传文件大小限制
        double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]);
        //遍历字符串数组把所有项加入项目编号控件
        for (int i = 0; i < arrFileTypeLimit.Length; i++)
        {
            bl_TileTypeLimit.Items.Add(arrFileTypeLimit[i].ToString());
        }
        //把文件大小限制赋值给标签
        lab_FileSizeLimit.Text = string.Format("{0:f2}M", iFileSizeLimit / 1024);
    }
    #endregion
    #region 初始化列表框控件文件列表信息
    private void InitFileList()
    {
        //从config中获取文件上传路径
        string strFileUpladPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
        //将虚拟路径转换为物理路径
        string strFilePath = Server.MapPath(strFileUpladPath);
        //读取上传文件夹下所有文件
        FileInfo[] arrFile = new DirectoryInfo(strFilePath).GetFiles();
        //把文件名逐一添加到列表框控件
        foreach(FileInfo fi in arrFile)
        {
            lb_FileList.Items.Add(fi.Name);
        }
    }
    #endregion
    #region 判断文件大小限制
    private bool IsAllowableFileSize()
    {
        //从web.config读取判断文件大小的限制
        double iFileSizeLimit = Convert.ToInt32(ConfigurationManager.AppSettings["FileSizeLimit"]) * 1024;
        //判断文件是否超出了限制
        if (iFileSizeLimit > FileUpload.PostedFile.ContentLength)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
    #region 判断文件类型限制
    protected bool IsAllowableFileType()
    {
        //从web.config读取判断文件类型限制
        string strFileTypeLimit = ConfigurationManager.AppSettings["FileTypeLimit"].ToString();
        //当前文件扩展名是否包含在这个字符串中
        if(strFileTypeLimit.IndexOf(Path.GetExtension(FileUpload.FileName).ToLower()) >0)
            return true;
        else
            return false;
    }
    #endregion
    #region 弹出警告消息
    protected void ShowMessageBox(string strMessage)
    {
        Response.Write(string.Format("<script>alert('{0}')</script>",strMessage));
    }
    #endregion
    #region 上传文件按钮事件
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        //判断用户是否选择了文件
        if (FileUpload.HasFile)
        {
            //调用自定义方法判断文件类型否符合
            if (IsAllowableFileType())
            {
                //判断文件大小是否符合
                if (IsAllowableFileSize())
                {
                    //从web.config中读取上传路径
                    string strFileUploadPath = ConfigurationManager.AppSettings["FileUplodePath"].ToString();
                    //从UploadFile控件中读取文件名
                    string strFileName = FileUpload.FileName;
                    //组合成物理路径
                    string strFilePhysicalPath = Server.MapPath(strFileUploadPath + "/") + strFileName;
                    //判断文件是否存在
                    if(!File.Exists(strFilePhysicalPath))
                    {
                        //保存文件
                        FileUpload.SaveAs(strFilePhysicalPath);
                        //更新列表框
                        lb_FileList.Items.Add(strFileName);
                        //更新文件夹信息
                        InitFolderInfo();
                        ShowMessageBox("上传成功!");
                    }
                    else
                    {
                        ShowMessageBox("文件已经存在!");
                    }
                }
                else
                {
                    ShowMessageBox("文件大小不符合要求!");
                }
            }
            else
            {
                ShowMessageBox("类型不匹配");
            }
        }
    }
    #endregion


共2页: 上一页 1 [2] 下一页
相 关 文 章   发布商链接
·asp.net操作xml文件的代码实例
·ASP.NET程序开发中经典常用的三十三...
·asp.net生成缩略图代码实例(可按百...
·在ASP.NET中实现跨页面多选的代码
·asp.net实现url传递中文的解决方案
·asp.net使用checkbox控制datagrid行...
·asp.net操作Excel,读取、导出,操作...
·asp.net页面回传与js调用服务端事件...
·asp.net中"已有打开的与此命令相关联...
·如何在asp.net中编写 Windows 服务程...
 §最新评论:(评论内容只代表网友观点,与本站立场无关!)
网名: 验证码:  【所有评论】【↑返回顶部
评 分: 12 345
评论内容:(不能超过500字,请自觉遵守互联网相关政策法规。[按 Ctrl+Enter 可直接提交]
注意:请勿在本站发布政治话题、色情及违反法律的内容。
IT知道网 声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。
推 荐 文 章
·实现多列,带图片的DropDownLi...
·javascript实现TreeView 控件
·ASP.NET配置Word的操作权限
·让FileUpload控件在IE和FireF...
·ASP.NET实现Office文档的分类
·asp.net文件下载显示进度条的
·实例讲解ASP.NET上传大文件的
·如何防止asp.net盗链下载问题
·分析asp.net页面后台执行的流
·asp.net实现的截取字符串函数...
·ASP.NET资源文件夹的具体介绍
·ASP.NET利用dot模板生成Word...
·图解asp.net如何用excel做报...
·ASP.NET中实现单点登录思路及
·一个可自动适应高度的TextBox...
热 门 文 章
·asp.net中Web.Config配置文件...
·Asp.Net防止刷新重复提交数据...
·图解asp.net如何用excel做报...
·asp.net页面回传与js调用服务...
·asp.net(C#版)实现登录验证码...
·ASP.NET取得物理路径和虚拟路...
·asp.net(c#)生成验证码代码,...
·ASP.net 实现批量数据更新或...
·DataGrid中DropDownList触发S...
·asp.net实现MSN弹出窗口特效...
·ViewState的工作原理及其示例...
·.net中C#代码与javaScript函...
·asp.net中常见的几种日历控件...
·vs无法在Web服务器上启动调试...
·DataGrid使用心得总结(附大量...
网站首页 - 关于本站 - 加入收藏 - 网站地图 - 友情连接 - 在线留言 - 联系我们 - 返回顶部
Copyright © 2007 IT知道网.[冀ICP备07026896号]. All Rights Reserved .