用户控件;

来源:互联网 发布:java开源文档管理系统 编辑:程序博客网 时间:2024/06/09 16:45

用户控件; 
  <%@  Control  Language="vb"  AutoEventWireup="false"  Codebehind="upfile.ascx.vb"  Inherits="fzjj.upfile"  TargetSchema="http://schemas.microsoft.com/intellisense/ie5"  %> 
  < s c r i p t  language="Java"> 
          function  addFile() 
          { 
          var  str  =  '<INPUT  type="file"  size="42"  NAME="File"    class="box_m"><br>' 
          document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str) 
          } 
  </> 
  <TABLE  width="100%"  align="left"  border="0"> 
  <TR> 
  <TD><input  class="mybutton"  onclick="addFile()"  type="button"  value="增加上传文件">  ?nbsp;
  <asp:button  id="btnReset"  Text="初始设置"  runat="server"></asp:button>?nbsp; <input  class="mybutton"  id="reset"  type="reset"  value="  清  除  "  name="Button"> 
  ?nbsp;
  <asp:button  id="btnUpload"  Text="立即上传"  runat="server"  Width="67px"></asp:button>?nbsp;
  </TD> 
  </TR> 
  <tr> 
  <td> 
  <P  id="MyFile"><INPUT  class="box_m"  style="HEIGHT:  22px"  type="file"  size="42"  name="File"> 
  <BR> 
  </P> 
  </td> 
  </tr> 
  </TABLE> 
  dll方法 
    '文件上传 
                  Public  Function  fileup() 
                          '//定义初始设置 
                          Dim  uploadPath  As  String  =  "upload"                    '定义上传的物理路径 
                          Dim  uploadType  As  String  =  ".rar,.doc,.txt"    '定义容许上传的文件类型 
                          Dim  maxFileSize  As  Integer  =  1024  *  5000          '设定最大5M; 
                          Dim  fullpath  As  String                                              '文件保存的物理路径 
                          Dim  fileextension  As  String                                    '文件的扩展名 
                          Dim  fileoldname  As  String                                        '文件的原名 
                          Dim  tempfilename  As  String                                      '文件的临时名字 
                          Dim  fullfilename  As  String                                      '文件的全名 
                          Dim  filenewname  As  String                                        '文件的新名字 
                          Dim  msg  As  String 
   
                          Dim  ds  As  New  DataSet("ds_file") 
                          Dim  dt  As  New  DataTable("upfile") 
                          Dim  i  As  Integer 
                          dt.Columns.Add("oldname",  System.Type.GetType("System.String")) 
                          dt.Columns.Add("newname",  System.Type.GetType("System.String")) 
                          ds.Tables.Add(dt) 
                          Dim  r  As  Random  =  New  Random 
                          Dim  ifile  As  Integer 
                          Dim  upfile  As  HttpPostedFile 
                          Dim  files  As  HttpFileCollection  =  System.Web.HttpContext.Current.Request.Files 
                          fullpath  =  HttpContext.Current.Request.PhysicalApplicationPath  +  uploadPath 
                          Try 
                                  For  ifile  =  0  To  files.Count  -  1 
                                          upfile  =  files(ifile) 
                                          fileoldname  =  System.IO.Path.GetFileName(upfile.FileName) 
                                          tempfilename  =  DateTime.Now.Year.ToString()  &  DateTime.Now.Month.ToString()  &  DateTime.Now.Day.ToString()  &  DateTime.Now.Minute.ToString()  &  DateTime.Now.Second.ToString() 
                                          tempfilename  =  tempfilename  &  r.Next(10000) 
                                          If  (fileoldname  <>  "")  Then 
                                                  fileextension  =  System.IO.Path.GetExtension(fileoldname).ToLower()  '取得上传文件的扩展名 
                                                  fullfilename  =  fullpath  &  "/"  &  tempfilename  &  fileextension              '保存文件的新文件全名" 
                                                  filenewname  =  tempfilename  &  fileextension                                                  '产生新文件名 
                                                  If  (uploadType.IndexOf(fileextension)  =  -1)  Then 
                                                          'msg  =  "< s c r i p t>alert('对不起,你要上传的文件不在许可的类型范围内!');history.back();</>" 
                                                  Else 
                                                          If  (upfile.ContentLength  >  maxFileSize)  Then 
                                                                  '  msg  =  "< s c r i p t>alert('对不起,文件超过限定大小!');history.back();</>" 
                                                          Else 
                                                                  '上传文件 
                                                                  upfile.SaveAs(fullfilename) 
                                                                  'msg  =  "< s c r i p t>alert('文件上传成功!');</>" 
                                                                  '  filearry.Item(ifile).Row(1).value  =  fileoldname 
                                                                  Dim  dr  As  DataRow  =  dt.NewRow() 
                                                                  dr(0)  =  fileoldname 
                                                                  dr(1)  =  fullfilename 
                                                                  dt.Rows.Add(dr) 
                                                          End  If 
                                                  End  If 
                                          End  If 
                                  Next 
                          Catch 
                                  '  msg  =  "< s c r i p t>alert('文件上传失败,请重试!');history.back();</>" 
                          End  Try 
                          Return  ds.Tables("upfile") 
                  End  Function 

页面应用  
  web.aspx

<%@  Register  TagPrefix="uc1"  TagName="upfile"  Src="upfile.ascx"  %> 
  <%@  Page  Language="vb"  AutoEventWireup="false"  Codebehind="WebForm1.aspx.vb"  Inherits="fzjj.WebForm1"%> 
  <!DOCTYPE  HTML  PUBLIC  "-//W3C//DTD  HTML  4.0  Transitional//EN"> 
  <HTML> 
  <HEAD> 
  <title>WebForm1</title> 
  <meta  content="Microsoft  Visual  Studio  .NET  7.1"  name="GENERATOR"> 
  <meta  content="Visual  Basic  .NET  7.1"  name="CODE_LANGUAGE"> 
  <meta  content="Java"  name="vs_defaultClient"> 
  <meta  content="http://schemas.microsoft.com/intellisense/ie5"  name="vs_targetSchema"> 
  </HEAD> 
  <body  MS_POSITIONING="GridLayout"> 
  <form  method="post"  encType="multipart/form-data"  runat="server"> 
  <table  width="100%"> 
  <tr> 
  <td><asp:panel  id="Panel1"  runat="server"  width="100%"> 
  <TABLE  style="WIDTH:  100%"  border=1> 
  <TR> 
  <TD>标  题:</TD> 
  <TD> 
  <asp:textbox  id="tb_name"  runat="server"  MaxLength="40"  Width="300px">Label</asp:textbox></TD> 
  </TR> 
  <TR> 
  <TD>内  容</TD> 
  <TD> 
  <asp:TextBox  id="neirong"  runat="server"  Width="504px"  Height="184px"  TextMode="MultiLine"></asp:TextBox></TD> 
  </TR> 
  <TR> 
  <TD>上传附件</TD> 
  <TD> 
  <uc1:upfile  id="Upfile1"  runat="server"></uc1:upfile></TD> 
  </TR> 
  <TR> 
  <TD>已上传附件</TD> 
  <TD> 
  <asp:DropDownList  id="t_upfile"  runat="server"></asp:DropDownList></TD> 
  </TR> 
  <TR> 
  <TD>分  类</TD> 
  <TD> 
  <asp:DropDownList  id="fenlei"  runat="server"  EnableViewState="true"  AutoPostBack="false"> 
  <asp:ListItem  Value="1">基本</asp:ListItem> 
  <asp:ListItem  Value="1">一般</asp:ListItem> 
  <asp:ListItem  Value="1">重要</asp:ListItem> 
  </asp:DropDownList></TD> 
  </TR> 
  </TABLE> 
  </asp:panel></td> 
  </tr> 
  </table> 
  </form> 
  </body> 
  </HTML> 
  web.vb 
  Public  Class  WebForm1 
          Inherits  System.Web.UI.Page 
          Dim  db  As  New  Web.config 
   
  #Region  "  Web  窗体设计器生成的代码  " 
   
          '该调用是  Web  窗体设计器所必需的。 
          <System.Diagnostics.DebuggerStepThrough()>  Private  Sub  InitializeComponent() 
   
          End  Sub 
          Protected  WithEvents  Panel1  As  System.Web.UI.WebControls.Panel 
          Protected  WithEvents  tb_name  As  System.Web.UI.WebControls.TextBox 
          Protected  WithEvents  fenlei  As  System.Web.UI.WebControls.DropDownList 
          Protected  WithEvents  neirong  As  System.Web.UI.WebControls.TextBox 
          Protected  WithEvents  t_upfile  As  System.Web.UI.WebControls.DropDownList 
   
          '注意:  以下占位符声明是  Web  窗体设计器所必需的。 
          '不要删除或移动它。 
          Private  designerPlaceholderDeclaration  As  System.Object 
   
          Private  Sub  Page_Init(ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  MyBase.Init 
                  'CODEGEN:  此方法调用是  Web  窗体设计器所必需的 
                  '不要使用代码编辑器修改它。 
                  InitializeComponent() 
          End  Sub     
  #End  Region 
          Private  Sub  Page_Load(ByVal  sender  As  System.Object,  ByVal  e  As  System.EventArgs)  Handles  MyBase.Load 
                  '在此处放置初始化页的用户代码 
                  t_upfile.DataSource  =  db.fileup 
                  t_upfile.DataTextField  =  "oldname" 
                  t_upfile.DataValueField  =  "newname" 
                  t_upfile.DataBind() 
          End  Sub 
  End  Class   

原创粉丝点击