惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AWS News Blog
AWS News Blog
Jina AI
Jina AI
量子位
V
V2EX
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园 - 【当耐特】
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
L
LangChain Blog
博客园_首页
aimingoo的专栏
aimingoo的专栏
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Engineering at Meta
Engineering at Meta
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
博客园 - 聂微东
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
NISL@THU
NISL@THU
Hacker News: Ask HN
Hacker News: Ask HN
腾讯CDC
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
H
Hacker News: Front Page
Webroot Blog
Webroot Blog
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Martin Fowler
Martin Fowler
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
雷峰网
雷峰网
PCI Perspectives
PCI Perspectives
月光博客
月光博客
SecWiki News
SecWiki News
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 绿毛虫

技巧/诀窍:在ASP.NET中重写URL(转) scrollLeft,scrollWidth,clientWidth,offsetWidth到底指的哪到哪的距离之完全详解 重写的支持多验证TextBox控件(转) asp.net网站统计(转) 呵呵 帮忙顶下咯 您需要掌握的八个CSS布局技巧(转) 采用DIV+CSS布局的好(转) 如何遍历Request的信息(转自孟子E章) 如何把html(form1)中的值用Submit传给serch.aspx页面,在serch.aspx又如何获取呢?(收集) 如何生成静态页面的五种方案(转) 在ASP.Net中使用FCKeditor FCKeditor 2.0 的设置.修改.使用(转) FckEditor中文配置手册详细说明(转) C#文件上传类(转) 服务器端FileUpload上传控件如何禁止手动输入(原) AJAX入门之深入理解JavaScript中的函数(转) C#文件上传类(转) asp.net利用RAR实现文件压缩解压缩[转载] C#加密解密方法 (转)
c#文件上传类(转)
绿毛虫 · 2007-09-06 · via 博客园 - 绿毛虫

using System;
using System.Web;
using System.IO;

namespace leo
{
 /// <summary>
 /// 文件上传类
 /// 作者:leo
 /// 制作时间:2006-2-13  最后修改于2007-6-25
 /// </summary>
 public class UploadFile
 {
        public UploadFile(HttpPostedFile PostedFile)
  {
            FileObj = PostedFile;
  }

  private HttpPostedFile FileObj;
  private int     maxSize         = 150 * 1024;             //默认150KB
  private string filesType        = ".jpeg|.jpg|.gif";      //默认文件类型
  private bool   oldFileName      = false;                  //是否采用原文件名
  private string filesPath        = "";                     //设置文件夹路径
  private string Eror             = "";                     //错误信息
  private int errorNum            = 0;                      //错误码

  private string savaFileName     = "";                     //上传成功后的文件名称


  //属性区
  public int MaxSizeKB
  {
   get{ return maxSize / 1024; }
   set{ maxSize = value * 1024;}
  }

  public string FilesType
  {
   get{ return filesType; }
   set{ filesType = value.ToLower();}
  }

  public string FilesPath
  {
   get{ return filesPath; }
   set{ filesPath = value;}
  }

  public string SavaFileName
  {
   get{ return savaFileName; }
   set{ savaFileName = value;}
  }

  public string SaveEror
  {
   get{ return Eror; }
  }

        public int ErrorNum
        {
            get { return errorNum; }
        }

  public bool OldFileName
  {
   get{ return oldFileName; }
   set{ oldFileName = value;}
  }

       
  //方法区
  private string GetNewFileName(string FlieExtension)
  {
   Random rd = new Random();
   int rnum = rd.Next(100, 999);
   string dt = DateTime.Now.ToString("yyyyMMddhhmmss");

   return dt + rnum.ToString() + FlieExtension;
  }

  public bool SaveFile()
  {
   if (FileObj != null)
   {
    try
    {
     string strFileURLName      = FileObj.FileName;                                      //上传文件原URL
     string strFileName         = Path.GetFileName(strFileURLName);              //上传文件原名
     string strFlieExtension    = Path.GetExtension(strFileURLName).ToLower();   //上传文件原扩展名


                    if (FileObj.ContentLength == 0)
                    {
                        Eror      = "您上传的文件为空";
                        errorNum  = 2;
                        return false;
                    }


     if (FileObj.ContentLength > maxSize)
     {
      Eror      = "上传文件超过限定大小";
                        errorNum  = 3;
      return false;
     }

                    string[] fType = filesType.Split('|');
                    bool ok = false;
                    for (int i = 0; i < fType.Length; i++)
                    {
                        if (fType[i] == strFlieExtension)
                            ok = true;
                    }
                    if (!ok)
                    {
                        Eror      = "上传文件类型不是程序允许的类型";
                        errorNum  = 4;
                        return false;
                    }

     if(!Directory.Exists(filesPath))
     {
      Eror      = "文件存储的目录不存在";
                        errorNum  = 5;
      return false;
     }

     string  NewFileName;

     if (!oldFileName)
     {
     NewFileName = GetNewFileName(strFlieExtension);
     }
     else
     NewFileName = strFileName;


     try
     {
      FileObj.SaveAs(Path.Combine(filesPath,NewFileName));
     }
     catch
     {
      Eror      = "存储文件时发生错误,可能是该目录没有权限";
                        errorNum  = 6;
      return false;
     }

     savaFileName = NewFileName;

                    Eror     = "";
                    errorNum = 0;
                    return true;
    }
    catch(Exception e)
    {
        Eror     = e.ToString();
                    errorNum = 9;
        return false;
    }
   
   }
   else
   {
    Eror     = "没有文件被上传";
                errorNum = 1;
    return false;
   }
  }

 }
}