用户名: 密码: 验证码:           网站地图 高级搜索 RSS订阅 收藏本站
Google
您的位置:首页>>网络编程>>.Net编程>>阅读资讯:ASP.NET程序上传文件功能的具体实例代码

ASP.NET程序上传文件功能的具体实例代码

[ 来源: | 阅读:次 | 更新日期:2007-10-6 14:20:17 | 评论 0 条 | 我要投稿 ]

//asp.net 中上传文件的源代码,测试成功!

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; 月落网

/// <summary>
/// Summary description for UploadFile
/// </summary>
public class UploadFile
{
    private string[] AllowFileType; //所允许的文件类型
    private double FileLength; //所允许的文件大小(KB)
    private string SavePath; //文件的存储路径
    private string SaveFile; //上传后的文件名
    private string Error; //存储错误信息
    private string FileExtesion; //上传文件的扩展名 字串5

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="allFileType">允许的文件类型,多个以","分开</param>
    /// <param name="fileLength">文件大小</param>
    /// <param name="savePath">保存路径</param>
    public UploadFile(string allFileType, double fileLength, string savePath)
    {
        char[] sp = { ',' };
        AllowFileType = allFileType.Split(sp);
        FileLength = fileLength * 1024;
        SavePath = savePath;
    }

字串6

    /// <summary>
    /// 返回生成的文件名
    /// </summary>
    public string FileName
    {
        get
        {
            return SaveFile;
        }
    } 字串5

    /// <summary>
    /// 返回出错信息
    /// </summary>
    public string ErrorMessage
    {
        get
        {
            return Error;
        }
    }

月落

    /// <summary>
    /// 根据SavePath,生成文件名
    /// </summary>
    /// <returns></returns>
    private string MakeFileName(string fileType,string fileName)
    {
        string file = this.SavePath + "\\" + DateTime.Now.ToString("yyMMddhhmmss") + fileName;
        while (File.Exists(file))
        {
            file = this.SavePath + "\\" + DateTime.Now.ToString("yyMMddhhmmss") + fileName;
        }
        return file;
    }

月落网

    /// <summary>
    /// 检查文件类型
    /// </summary>
    /// <param name="fileEx">MIME内容</param>
    /// <returns></returns>
    private bool CheckFileExt(string fileEx)
    {
        bool result = false;
        for (int i = 0; i < this.AllowFileType.Length; i++)
        {
            if (fileEx.IndexOf(this.AllowFileType[i].ToLower()) > -1)
            {
                result = true;
                break;
            } 字串8
        }
        return result;
    } 字串9

    public bool UpLoad(System.Web.UI.WebControls.FileUpload file)
    {
        bool result = true;
        try
        {
            //查看文件长度
            if (file.PostedFile.ContentLength > (this.FileLength))
            {
                this.Error = "文件大小超出范允许的范围!";
                return false;
            } 字串6

            string fileName = Path.GetFileName(file.PostedFile.FileName);
            this.FileExtesion = Path.GetExtension(fileName); 字串9

            if (!CheckFileExt(this.FileExtesion.ToLower()))
            {
                this.Error = "文件类型" + this.FileExtesion + "不允许!";
                return false;
            }
            //取得要保存的文件名
            string UpFile = this.MakeFileName(this.FileExtesion, fileName);
            //保存文件
            file.PostedFile.SaveAs(UpFile); 字串9

            //返回文件名
            this.SaveFile = Path.GetFileName(UpFile); 字串8

        }
        catch (Exception e)
        {
            result = false;
            this.Error = e.Message;
        }
        return result;
    }

www.yueluo.net

}

www.yueluo.net


Tags:ASP.NET,上传文件
责任编辑:
您的评论
用户名:新注册) 密码: 匿名评论 [所有评论]

·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为