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

推荐订阅源

cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Forbes - Security
Forbes - Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
W
WeLiveSecurity
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
Engineering at Meta
Engineering at Meta
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
T
The Exploit Database - CXSecurity.com
F
Full Disclosure
WordPress大学
WordPress大学
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
SegmentFault 最新的问题
P
Privacy International News Feed
IT之家
IT之家
M
MIT News - Artificial intelligence
G
GRAHAM CLULEY
Hacker News: Ask HN
Hacker News: Ask HN
D
DataBreaches.Net
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
Check Point Blog
美团技术团队
Security Latest
Security Latest
Cyberwarzone
Cyberwarzone
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
H
Help Net Security
宝玉的分享
宝玉的分享
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
The Cloudflare Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
I
InfoQ
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog

博客园 - 绿毛虫

技巧/诀窍:在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#文件上传类(转) C#文件上传类(转) 服务器端FileUpload上传控件如何禁止手动输入(原) AJAX入门之深入理解JavaScript中的函数(转) asp.net利用RAR实现文件压缩解压缩[转载] C#加密解密方法 (转)
C#文件上传类(转)
绿毛虫 · 2007-09-05 · via 博客园 - 绿毛虫

using System;

namespace UpFile { ///
/// upfile 的摘要说明。
///
public class upfile
{
  private string path = null;
  private string fileType = null;
  private int sizes = 0;
  ///
  /// 初始化变量
  ///
  public upfile()
  {
   path = @"\uploadimages\"; //上传路径
   fileType = "jpg|gif|bmp";
   sizes = 200; //传文件的大小,默认200KB
  }

  ///
  /// 设置上传路径,如:uploadimages\
  ///
  public string Path
  {
   set
   {
    path = @"\" + value + @"\";
   }
  }

  ///
  /// 设置上传文件大小,单位为KB
  ///
  public int Sizes
  {
   set
   {
    sizes = value * 1024;
   }
  }

  ///
  /// 设置上传文件的类型,如:jpg|gif|bmp ///
  public string FileType
  {
   set
   {
    fileType = value;
   }
  }

  ///
  /// 上传图片
  ///
  /// 上传控件名称
  /// true则以当前时间创建文件夹,false则为设置的文件夹
  /// 返回上传图片的相对路径
  public string fileSaveAs(System.Web.UI.HtmlControls.HtmlInputFile name,bool creatDirectory)
  {
   try
   {
    string filePath=null;
    //以当前时间修改图片的名字或创建文件夹的名字
    string modifyFileName = 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();
    //获得站点的物理路径
    string uploadFilePath = null;
    //如果为true则以当前时间创建文件夹,否则为设置的文件夹
    if(creatDirectory)
    {
     uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + @"\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + @"\";
    }
    else
    {
     uploadFilePath = System.Web.HttpContext.Current.Server.MapPath(".") + path;
    }
    //获得文件的上传的路径
    string sourcePath=name.Value.Trim();
    //判断上传文件是否为空
    if(sourcePath == "" || sourcePath == null)
    {
     message("您没有上传数据呀,是不是搞错了呀!");
     return null;
    }
    //获得文件扩展名
    string tFileType = sourcePath.Substring(sourcePath.LastIndexOf(".")+1);
    //获得上传文件的大小
    long strLen = name.PostedFile.ContentLength;
    //分解允许上传文件的格式
    string[] temp = fileType.Split('|');
    //设置上传的文件是否是允许的格式
    bool flag = false;
    //判断上传文件大小
    if(strLen >= sizes)
    {
    
     message("上传的图片不能大于" + sizes + "KB");
     return null;
    }
    //判断上传的文件是否是允许的格式
    foreach(string data in temp)
    {
     if(data == tFileType)
     {
      flag = true ;
      break;
     }
    }
    //如果为真允许上传,为假则不允许上传
    if(!flag)
    {
     message("目前本系统支持的格式为:"+fileType);
     return null;
    }
    System.IO.DirectoryInfo dir=new System.IO.DirectoryInfo(uploadFilePath);
    //判断文件夹否存在,不存在则创建
    if(!dir.Exists)
    {
     dir.Create();
    }
    filePath = uploadFilePath + modifyFileName + "." + tFileType;
    name.PostedFile.SaveAs(filePath);
    filePath = path + modifyFileName + "." + tFileType;

    return filePath;

   }
   catch
   {
    //异常
    message("出现未知错误!");
    return null;
   }
  }

  private void message(string msg,string url)
  {
   System.Web.HttpContext.Current.Response.Write(" alert('"+msg+"');window.location='"+url+"' ");
  }

  private void message(string msg)
  {
   System.Web.HttpContext.Current.Response.Write(" alert('"+msg+"'); ");
  }
}
}