自己写的文件上传按钮

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using levin;

namespace levin.Files
{
 /// <summary>
 /// UpDown 的摘要说明。
 /// </summary>
 public class UpDown
 {
  public UpDown()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  public static bool IsAllowedExtension(HtmlInputFile hifile)
  {
   string strOldFilePath = "",strExtension = "";

   //允许上传的扩展名,可以改成从配置文件中读出
   string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};

   if(hifile.PostedFile.FileName != string.Empty)
   {
    strOldFilePath = hifile.PostedFile.FileName;
    //取得上传文件的扩展名
    strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
    //判断该扩展名是否合法
    for(int i = 0; i< arrExtension.Length; i++)
    {
     if(strExtension.Equals(arrExtension[i]))
     {
      return true;
     }
    }
   }       
   return false;
  }
  public static bool IsAllowedExtension(string  FileType)
  {
   //string strOldFilePath = ""
   //string strExtension = "";

   //允许上传的扩展名,可以改成从配置文件中读出
   string[] arrExtension = {".gif",".jpg",".jpeg",".bmp",".png"};

   if(FileType != string.Empty)
   {    
    //判断该扩展名是否合法
    for(int i = 0; i< arrExtension.Length; i++)
    {
     if(FileType.Equals(arrExtension[i]))
     {
      return true;
     }
    }
   }       
   return false;
  }

  public static bool IsAllowedLength(HtmlInputFile hifile)
  {
   //允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
   int i = 20;
   //如果上传文件的大小超过最大值,返回flase,否则返回true.
   if(hifile.PostedFile.ContentLength > i * 1024)
   {
    return false;
   }
   return true;
  }
  public static bool IsAllowedLength(string filesize)
  {
   //允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
   int i = 20;
   //如果上传文件的大小超过最大值,返回flase,否则返回true.
   if(int.Parse(filesize) > i * 1024)
   {
    return false;
   }
   return true;
  }
  public static bool IsAllowedLength(int filesize)
  {
   //允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
   int i = 20;
   //如果上传文件的大小超过最大值,返回flase,否则返回true.
   if(filesize > i * 1024)
   {
    return false;
   }
   return true;
  }


  public static FileUpModem SaveFile(HtmlInputFile fileUp,string strAbsolutePath)
  {
   FileUpModem mod=new FileUpModem();
   string strOldFilePath = "",strExtension = "",strNewFileName = "",mFileName="";

   if(fileUp.PostedFile.FileName != string.Empty)
   {
    strOldFilePath = fileUp.PostedFile.FileName;
    //取得上传文件的扩展名
    strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
    //文件上传后的命名
    strNewFileName = util.ApiTime.DateTimeFormat() + strExtension;
    mFileName=strOldFilePath.Substring(strOldFilePath.LastIndexOf("//")+1);//取得文件名 

    mod.FileType=strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
    mod.Size=Convert.ToString(fileUp.PostedFile.ContentLength);
    mod.Name=strOldFilePath.Substring(0,mFileName.LastIndexOf(".")) +util.ApiTime.DateTimeFormat() + mod.FileType;

    if(strAbsolutePath.LastIndexOf("//") == strAbsolutePath.Length)
    {
     fileUp.PostedFile.SaveAs(strAbsolutePath + strNewFileName);   
    }
    else
    {
     fileUp.PostedFile.SaveAs(strAbsolutePath + "//" + strNewFileName);   
    }
   }
   return mod;
  }


  public static FileUpModem UploadFile(System.Web.UI.HtmlControls.HtmlInputFile fileUp,string path)
  {
   FileUpModem mod=new FileUpModem();
   string filePath="",fileExtName="",mFileName,mPath,sPath;
   
   if("" != fileUp.PostedFile.FileName)
   {
    filePath =fileUp.PostedFile.FileName;//取得文件路径
    fileExtName= filePath.Substring(filePath.LastIndexOf(".")+1);
    try
    {
     //取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。     
     sPath=HttpContext.Current.Server.MapPath("upload/");     
     mPath=(path=="0")? sPath :sPath + path + @"/";
     mFileName=filePath.Substring(filePath.LastIndexOf("//")+1);//取得文件名 
     //设置文件模型
     mod.FileType=mFileName.Substring(mFileName.LastIndexOf("."));
     mod.Size=Convert.ToString(fileUp.PostedFile.ContentLength);
     mod.Name=mFileName.Substring(0,mFileName.LastIndexOf(".")) +util.ApiTime.DateTimeFormat() + mod.FileType;
     //保存上传文件到指定的目录
     //levin.api.WebResponse.alert( mPath + mFileName);     
     if (!IsAllowedExtension(mod.FileType)) api.WebResponse.alertGoBack(Const.Mess_FileTypeNoAllow);
     if (!IsAllowedLength(mod.Size)) api.WebResponse.alertGoBack(Const.Mess_FileLengthNoAllow);

     fileUp.PostedFile.SaveAs(mPath + mod.Name );
     //设置文件模型
     mod.PrevName=mFileName;     
     mod.AbsolutePath ="/upload/" + path + @"/" + mod.Name;
     mod.CurrentPath = mPath + mFileName;
     mod.HttpPath=Files.FileApi.AbsPath(mod.Name);
     //HttpContext.Current.Response.Write(levin);
     HttpContext.Current.Response.Write(mod.AbsolutePath);
     levin.api.WebResponse.alert(Const.Mess_UploadSuccess);
    }
    catch
    {
     levin.api.WebResponse.alert(Const.Mess_UploadFalse);     
    }
   }
   return mod;
  }
 }
 public class FileUpModem
 {
  private string strSize;
  private string strName;
  private string strPrevName;
  private string strAbsolutePath;
  private string strCurrentPath;
  private string strFileType;
  private string strHttpPath;
  
  public string HttpPath
  {
   get {return strHttpPath;}
   set {strHttpPath=value;}  
  }
  public string FileType
  {
   get {return strFileType;}
   set {strFileType=value;}  
  }
  public string Size
  {
   get {return strSize;}
   set {strSize=value;}  
  }
  public string Name
  {
   get {return strName;}
   set {strName=value;}  
  }
  public string PrevName
  {
   get {return strPrevName;}
   set {strPrevName=value;}  
  }
  /// <summary>
  /// 相对路径
  /// </summary>
  public string AbsolutePath
  {
   get {return strAbsolutePath;}
   set {strAbsolutePath=value;}  
  }
  public string CurrentPath
  {
   get {return strCurrentPath;}
   set {strCurrentPath=value;}  
  }

 }
}

 

demo

 <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="levin.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 280px; POSITION: absolute; TOP: 248px" runat="server"
    Text="Button"></asp:Button>
   <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 336px; POSITION: absolute; TOP: 104px" runat="server"
    Text="发送电子邮件"></asp:Button>
   <INPUT id="FileUp" runat="server" style="Z-INDEX: 103; LEFT: 336px; POSITION: absolute; TOP: 56px"
    type="file">
   <asp:Button id="Button3" style="Z-INDEX: 104; LEFT: 576px; POSITION: absolute; TOP: 56px" runat="server"
    Text="Button"></asp:Button>
   <asp:Button id="Button4" style="Z-INDEX: 105; LEFT: 656px; POSITION: absolute; TOP: 48px" runat="server"
    Text="read file"></asp:Button>
   <asp:TextBox id="TextBox1" style="Z-INDEX: 106; LEFT: 352px; POSITION: absolute; TOP: 168px"
    runat="server" Width="168px" Height="96px" TextMode="MultiLine"></asp:TextBox>
   <asp:Button id="Button5" style="Z-INDEX: 107; LEFT: 600px; POSITION: absolute; TOP: 232px" runat="server"
    Text="write file"></asp:Button>
   <asp:Button id="Button6" style="Z-INDEX: 108; LEFT: 584px; POSITION: absolute; TOP: 160px" runat="server"
    Text="append file"></asp:Button>
   <asp:Button id="Button7" style="Z-INDEX: 109; LEFT: 368px; POSITION: absolute; TOP: 320px" runat="server"
    Text="read liu file"></asp:Button>
   <asp:Button id="Button8" style="Z-INDEX: 110; LEFT: 312px; POSITION: absolute; TOP: 424px" runat="server"
    Text=" cn rili  "></asp:Button>
   <asp:Button id="Button9" style="Z-INDEX: 111; LEFT: 520px; POSITION: absolute; TOP: 368px" runat="server"
    Text="读取文件"></asp:Button>
  </form>
 </body>
</HTML>

 

cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using levin;
namespace levin
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Button Button3;
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Button Button4;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Button Button5;
  protected System.Web.UI.WebControls.Button Button6;
  protected System.Web.UI.WebControls.Button Button7;
  protected System.Web.UI.WebControls.Button Button8;
  protected System.Web.UI.WebControls.Button Button9;
  protected System.Web.UI.HtmlControls.HtmlInputFile FileUp;
  
  
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Button3.Click += new System.EventHandler(this.Button3_Click);
   this.Button4.Click += new System.EventHandler(this.Button4_Click);
   this.Button5.Click += new System.EventHandler(this.Button5_Click);
   this.Button6.Click += new System.EventHandler(this.Button6_Click);
   this.Button7.Click += new System.EventHandler(this.Button7_Click);
   this.Button8.Click += new System.EventHandler(this.Button8_Click);
   this.Button9.Click += new System.EventHandler(this.Button9_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   levin.api.WebResponse.alert("aaa","WebForm2.aspx");
  }

  private void Button2_Click(object sender, System.EventArgs e)
  {
   //levin.Jmail.SendMail("blue_lzg@163.com","555555","levin9@gmail.com","title","gggg");
   levin.util.Jmail.SendMail("blue_lzg@163.com","555555","gtips@163.com","title","gggg");
  }

  private void Button3_Click(object sender, System.EventArgs e)
  {
   Files.UpDown.UploadFile(FileUp,"test");
   //levin.File.UpDown.SaveFile(FileUp,"text");
  }

  private void Button4_Click(object sender, System.EventArgs e)
  {
   Response.Write(Files.FileApi.Read(FileUp));
  }

  private void Button5_Click(object sender, System.EventArgs e)
  {
   Files.FileApi.write(FileUp,TextBox1.Text);
  }

  private void Button6_Click(object sender, System.EventArgs e)
  {
   Files.FileApi.Append(FileUp,TextBox1.Text);
  }

  private void Button7_Click(object sender, System.EventArgs e)
  {
   Response.Write(Files.FileApi.Reads(FileUp));
  }

  private void Button8_Click(object sender, System.EventArgs e)
  {
   util.CNDate dt = new levin.util.CNDate(DateTime.Now);
   Response.Write("今天是:" + dt.Date.ToString() + dt.GetConstellationName());
   Response.Write(dt.l_GetLunarHolDay());
   Console.WriteLine(dt.GetLunarHolDay());
//   Console.WriteLine("闰月" + dt.GetLeapMonth(UInt16.Parse(args[0])));
//   Console.WriteLine("2月的天数" + dt.LunarMonthDays(UInt16.Parse(args[0]), UInt16.Parse(args[1])));
//   Console.WriteLine("天数" + dt.LunarYearDays(UInt16.Parse(args[0])));
   Response.Write("" + dt.FormatLunarYear());
   dt.Date = DateTime.Today.AddDays(1);
   Response.Write("明天是:" + dt.Date.ToString() + dt.GetConstellationName());
  }

  private void Button9_Click(object sender, System.EventArgs e)
  {
   TextBox1.Text=Files.FileApi.Read(FileUp).ToString();
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值