shawl.qiu c# .net upload class v1.1

介绍了一个简单易用的C# .NET上传组件,该组件适用于自动化画廊程序等场景,支持多种文件格式,提供了丰富的配置选项如自动重命名、上传间隔设置等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

shawl.qiu c# .net upload class v1.1 

 没什么好说的, .NET 干这个真太容易了...
 
 这几天在写一个自动化画廊程序, 需要用到上传功能, 就重写了一下先前写的上传类,  使其使用更方便更简洁...

 目录:
 1. x.aspx
 2. cs/upload.cs
 
 下载:
  http://files.myopera.com/btbtd/csharp/class/sq_csharp_upload_v1.1.7z
 
 shawl.qiu
 2007-02-08
 http://blog.youkuaiyun.com/btbtd 

 1. x.aspx
  1. <%@ Page Language="C#AutoEventWireup="True" %>
  2. <%@ Assembly src="cs/upload.cs" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <meta http-equiv="Content-Typecontent="text/html; charset=utf-8" />
  7. <title>shawl.qiu template</title>
  8. </head>
  9. <body>
  10. <script runat="server">
  11.  void Page_Load(Object s, EventArgs e)
  12.  {
  13.   upload up=new upload();
  14.   
  15.   up.ext="7z,rar,zip,mp3,bmp,gif,jpg,jpeg,png,txt,swf";
  16.   up.debug=false;
  17.   up.item=5;                      // 声明显示多少个上传框
  18.   up.path="/uploads/";            // 上传目录;
  19.   up.goback_url=Request.Url+"";   // 上传完毕后返回的 URL
  20.   up.goback_second=5;             // 上传后返回间隔
  21.   up.interval=5;                  // 每次上传间隔, 单位秒
  22.   up.autorename=true;             // 自动重命名;
  23.   
  24.   // 声明 处理 所有 返回路径 的方法
  25.   //up.ReturnFilePath=new OneArgStringDelegate(GetPath);
  26.   
  27.   up.UploadPh=sqUpload;           // 声明显示上传控件的占位符
  28.   up.UploadBox();                 // 显示上传操作控件
  29.   
  30.   up=null;
  31.  }
  32.  
  33.  public void GetPath(String sPath)
  34.  {
  35.   System.Web.HttpContext.Current.Response.Write(sPath);
  36.  }
  37.  
  38. </script>
  39.  <form runat="server">
  40.   <asp:PlaceHolder id="sqUploadrunat="server" />
  41.  </form>
  42. </body>
  43. </html>
  44.  
 2. cs/upload.cs
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.HtmlControls;

  9. public delegate void OneArgStringDelegate(String Str);

  10. /*-----------------------------------------------------------------------------------*/
  11.  * shawl.qiu c# .net upload class v1.1
  12. /*-----------------------------------------------------------------------------------*/
  13. //---------------------------------------------------------------------begin class upload
  14. public class upload
  15. {
  16.  //-----------------------------------begin event
  17.  public upload()
  18.  {
  19.  }
  20.  
  21.  ~upload()
  22.  {
  23.  }
  24.  //-----------------------------------end event
  25.  
  26.  //-----------------------------------begin public constant
  27.  //-----------------------begin about
  28.  public const string auSubject="shawl.qiu c# .net upload class";
  29.  public const string auVersion="v1.1";
  30.  public const string au="shawl.qiu";
  31.  public const string auEmail="shawl.qiu@gmail.com";
  32.  public const string auBlog="http://blog.youkuaiyun.com/btbtd";
  33.  public const string auCreateDate="2007-1-30";
  34.  public const string auFirstUpdate="2007-2-8 v1.1";
  35.  //-----------------------end about
  36.  //-----------------------------------end public constant
  37.  
  38.  //-----------------------------------begin public variable
  39.  public Boolean debug=false;
  40.  public Boolean autorename=false;
  41.  
  42.  public String path="/uploads/";
  43.  public String ext="7z,rar,zip,mp3,bmp,gif,jpg,jpeg,png,txt,swf";
  44.  public String goback_url="?";
  45.  
  46.  public Int32 interval=10;
  47.  public Int32 goback_second=10;
  48.  public Int32 item=5;
  49.  public Int32 totalsize=1024*5;
  50.  public Int32 singlesize=1024;
  51.  
  52.  public PlaceHolder UploadPh;
  53.  
  54.  public OneArgStringDelegate ReturnFilePath;
  55.  
  56.  //-----------------------------------end public variable
  57.  
  58.  //-----------------------------------begin public method
  59.  
  60.  public void UploadBox()
  61.  {
  62.   if(UploadPh!=null)
  63.   {
  64.    UploadPh.Controls.Add(ErrorLabel);
  65.    UploadPh.Controls.Add(GobackLabel);
  66.    UploadPh.Controls.Add(UploadDetailsLabel);
  67.    
  68.    InfoLabel=new Label();
  69.    InfoLabel.Text="<ul>/n"+
  70.     "<li>允许上传的文件类型<b>: "+ext+"</b></li>/n"+
  71.     "<li>每次总上传大小最大为 <b>: "+totalsize+"kb</b>, 每文件最大为 <b>"+
  72.     singlesize+"kb</b>.</li>/n"+
  73.     "<li>每次上传时间间隔为<b>: "+interval+"</b>秒.</li>/n"+
  74.     "</ul>";
  75.      
  76.    UploadPh.Controls.Add(InfoLabel);
  77.     
  78.     LiteralBox=new Literal();
  79.     LiteralBox.Text="<ol>";
  80.     UploadPh.Controls.Add(LiteralBox);
  81.    
  82.    for(Int32 i=0; i<item; i++)
  83.    {
  84.     LiteralBox=new Literal();
  85.     LiteralBox.Text="<li>";
  86.     UploadPh.Controls.Add(LiteralBox);
  87.     
  88.     UpInputItem = new HtmlInputFile(); 
  89.     UploadPh.Controls.Add(UpInputItem);
  90.     
  91.     LiteralBox=new Literal();
  92.     LiteralBox.Text="</li>/n";
  93.     UploadPh.Controls.Add(LiteralBox);
  94.    }     
  95.     
  96.    LiteralBox=new Literal();
  97.    LiteralBox.Text="</br/>/n";
  98.    UploadPh.Controls.Add(LiteralBox);
  99.    
  100.    SubmitButton=new Button();
  101.    SubmitButton.Text="Upload now";
  102.    SubmitButton.Click+=new EventHandler(go);
  103.    UploadPh.Controls.Add(SubmitButton);
  104.     
  105.    LiteralBox=new Literal();
  106.    LiteralBox.Text=" <input type=/"reset/" value=/"reset/" />";
  107.    UploadPh.Controls.Add(LiteralBox);
  108.     
  109.    LiteralBox=new Literal();
  110.    LiteralBox.Text="</ol>/n";
  111.    UploadPh.Controls.Add(LiteralBox);
  112.   }
  113.  } // end public void UploadBox
  114.  
  115.  public void go(Object s, EventArgs e)
  116.  {
  117.   
  118.   if(HttpContext.Current.Session["sqUpFl"]==null)
  119.   {
  120.    HttpContext.Current.Session["sqUpFl"]=DateTime.Now;
  121.   }
  122.   
  123.   DateTime dtSession=DateTime.Parse(HttpContext.Current.Session["sqUpFl"]+"");

  124.   if(dtSession>DateTime.Now)
  125.   {
  126.    TimeSpan hasInterval=dtSession-DateTime.Now;
  127.    
  128.    finished("每次上传时间间隔为 "+interval+" 秒, 请稍候...",
  129.     hasInterval.Seconds, goback_url, GobackLabel);
  130.    goto End;
  131.   }
  132.   
  133.   //---------------------检测上传目录是否存在
  134.   String path_phs=HttpContext.Current.Server.MapPath(path);
  135.   if(!Directory.Exists(path_phs))
  136.   {
  137.    HttpContext.Current.
  138.     Response.Write("<h2>指定的上传目录不存在, 操作被取消.</h2>");
  139.    goto End;
  140.   }
  141.   
  142.   Int32 upTotal=HttpContext.Current.Request.ContentLength/1024;
  143.   Int32 total=HttpContext.Current.Request.Files.AllKeys.Length;
  144.   
  145.   ArrayList aUpsize=new ArrayList();
  146.   ArrayList aNoExt=new ArrayList();
  147.   ArrayList aNotAllowExt=new ArrayList();
  148.   ArrayList aFinished=new ArrayList();
  149.   
  150.   if(upTotal>totalsize) goto Upsize;
  151.    
  152.   for(Int32 i=0; i<total; i++)
  153.   {
  154.    System.Web.HttpPostedFile files=HttpContext.Current.Request.Files[i];
  155.    if(files.ContentLength>0)
  156.    { 
  157.     String flnm=Path.GetFileName(files.FileName);
  158.     String justnm=Path.GetFileNameWithoutExtension(files.FileName);
  159.     String justext=Path.GetExtension(files.FileName);
  160.     
  161.     String flph=path+justnm+justext;
  162.     String flph_phs=HttpContext.Current.Server.MapPath(flph);
  163.     
  164.     //-----------------------------------单文件超出限制大小
  165.     Int32 flSize=files.ContentLength/1024;
  166.     if(flSize>singlesize)
  167.     {
  168.      aUpsize.Add("文件: "+flnm+", 大小为: "+flSize+" kb.");
  169.      continue;
  170.     }    
  171.     
  172.     if(!Path.HasExtension(flnm))
  173.     {
  174.      aNoExt.Add("文件: "+flnm+", 没有扩展名.");
  175.      continue;
  176.     }
  177.     
  178.     String extTemp=Regex.Replace(justext,"^.","");
  179.     if(!Regex.IsMatch(ext, "//b"+extTemp+"//b", RegexOptions.IgnoreCase))
  180.     {
  181.      aNotAllowExt.Add("不允许上传的文件扩展: "+flnm);
  182.      continue;
  183.     }
  184.     
  185.     if(File.Exists(flph_phs)) //------------------已存在相同同名文件
  186.     {
  187.      if(autorename)
  188.      { 
  189.       Int32 iRename=1;
  190.       while(true)
  191.       {
  192.        String sNameTemp=HttpContext.Current.
  193.         Server.MapPath(path+justnm+"_"+(iRename++)+justext);
  194.        if(!File.Exists(sNameTemp))
  195.        {
  196.         flph_phs=sNameTemp;
  197.         goto SaveFile;
  198.        } // end if 3
  199.       } // end while
  200.      } // end if 2
  201.     } // end if 1
  202.     SaveFile:;
  203.      files.SaveAs(flph_phs);
  204.   
  205.      String flnmTemp=Path.GetFileName(flph_phs);
  206.      aFinished.Add("文件: "+flnmTemp+" 已上传.");
  207.      
  208.      allFilePath+=path+flnmTemp+",";
  209.      
  210.      HttpContext.Current.Session["sqUpFl"]=
  211.       DateTime.Now.AddSeconds(interval);
  212.    } // end if
  213.   } // end for
  214.   goto Report;
  215.   
  216.   Upsize:
  217.    ErrorLabel.Text+="<h2 style='text-align:center;'>上传大小超出限制, 已被终止.</h2>";
  218.    finished(goback_second+" 秒后返回, 还有 ", 
  219.     goback_second, goback_url, GobackLabel);
  220.   goto End;
  221.    
  222.   Report:
  223.    listAl("已上传文件 ", aFinished, UploadDetailsLabel);
  224.    listAl("单文件超出限制大小的有 ", aUpsize, UploadDetailsLabel);
  225.    listAl("没有文件扩展名的有 ", aNoExt, UploadDetailsLabel);
  226.    listAl("不允许上传的文件扩展有 ", aNotAllowExt, UploadDetailsLabel);
  227.   goto Finished;
  228.   
  229.   Finished:
  230.    finished("操作已完毕, "+goback_second+" 秒后返回, 还有 ", 
  231.     goback_second, goback_url, GobackLabel);
  232.   End: 
  233.    allFilePath=Regex.Replace(allFilePath,",$","");
  234.    
  235.    if(ReturnFilePath!=null)
  236.    { 
  237.     ReturnFilePath(allFilePath);
  238.    }
  239.  } // end go
  240.  //-----------------------------------end public method
  241.  
  242.  //-----------------------------------begin private variable
  243.  private String allFilePath="";
  244.  
  245.  private Label InfoLabel;
  246.  private Label GobackLabel=new Label();
  247.  private Label UploadDetailsLabel=new Label();
  248.  private Label ErrorLabel=new Label();
  249.  
  250.  private Literal LiteralBox;
  251.  
  252.  private Button SubmitButton;
  253.  
  254.  private HtmlInputFile UpInputItem;
  255.  //-----------------------------------end private variable
  256.  
  257.  //-----------------------------------begin private method
  258.  private void listAl(String sDetail, ArrayList oAl, Label UploadDetailsLabel)
  259.  {
  260.   if(oAl.Count>0)
  261.   {
  262.    UploadDetailsLabel.Text="<ol><label>"+sDetail+"<b>"+
  263.     oAl.Count+"个</b>.</label>";
  264.    foreach(Object item in oAl)
  265.    {
  266.     UploadDetailsLabel.Text+="<li>"+item+"</li>";
  267.    }
  268.    UploadDetailsLabel.Text+="</ol><hr/>";
  269.   }
  270.  }
  271.  
  272.  private void finished(String sPrompt, Int32 iSecond, String sUrl, Label GobackLabel)
  273.  {
  274.   GobackLabel.Text+="<script type=/"text/javascript/">/n";
  275.   GobackLabel.Text+="//<![CDATA[/n";
  276.   GobackLabel.Text+=var temp=onload;/n";
  277.   GobackLabel.Text+=onload=function(){/n";
  278.   GobackLabel.Text+="  try{temp()}catch(e){}/n";
  279.   GobackLabel.Text+="  fTimer("+iSecond+",'timer', 10);/n";
  280.   GobackLabel.Text+=" }/n";
  281.   GobackLabel.Text+="  function fTimer(iTimestamp, sId, iMs){/n";
  282.   GobackLabel.Text+="  if(!(iTimestamp.constructor==Date)){/n";
  283.   GobackLabel.Text+="   var sqTimeStamp=new Date();/n";
  284.   GobackLabel.Text+="   sqTimeStamp.setSeconds(sqTimeStamp.getSeconds()+iTimestamp);/n";
  285.   GobackLabel.Text+="   iTimestamp=sqTimeStamp;/n";
  286.   GobackLabel.Text+="  }/n";
  287.   GobackLabel.Text+="    var tl=arguments.callee;/n";
  288.   GobackLabel.Text+="    if(typeof sId=='string'){/n";
  289.   GobackLabel.Text+="   var oEle=document.getElementById(sId);/n";
  290.   GobackLabel.Text+="  } else {/n";
  291.   GobackLabel.Text+="   var oEle=sId;/n";
  292.   GobackLabel.Text+="  }/n";
  293.   GobackLabel.Text+="  var dt=new Date();/n";
  294.   GobackLabel.Text+="  var iCk=((iTimestamp.getTime()-dt.getTime())/1000).toFixed(3);/n";
  295.   GobackLabel.Text+="  if(iCk<=0){/n";
  296.   GobackLabel.Text+="   oEle.innerHTML='00.000';/n";
  297.   GobackLabel.Text+="   return false;/n";
  298.   GobackLabel.Text+="  } else {/n";
  299.   GobackLabel.Text+="   oEle.innerHTML=iCk;/n";
  300.   GobackLabel.Text+="   var iTimer=setTimeout(function(){tl(iTimestamp, oEle, iMs)},iMs); /n";
  301.   GobackLabel.Text+="  }/n";
  302.   GobackLabel.Text+=" } // end function fTimer // shawl.qiu script/n";
  303.   GobackLabel.Text+="//]]>/n";
  304.   GobackLabel.Text+="</script>/n";

  305.   GobackLabel.Text+="<meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/" />";
  306.   GobackLabel.Text+="<meta http-equiv=/"refresh/" content=/""+iSecond+";URL="+sUrl+"/">";
  307.   GobackLabel.Text+="<div style=/"display:table;width:100%;background-color:yellow!important;"+
  308.     "color:black!important;text-align:center!important;/">"+
  309.     sPrompt+", <span id='timer'>"+iSecond+"</span>秒 后返回.</div>";
  310.  }
  311.  //-----------------------------------end private method
  312. }
  313. //---------------------------------------------------------------------end class upload
  314.  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值