在ASP.NET中实现多文件上传[1]

[入库:2005年8月18日] [更新:2007年3月27日]

本文简介:选择自 yllaji 的 blog

在asp.net中实现多文件上传 
作者: 孟宪会 出自: 【孟宪会之精彩世界】 发布日期: 2003-5-20 23:41;:07;
--------------------------------------------------------------------------------
 
在以前的web应用中,上传文件是个很麻烦的事,现在有了.net,文件上传变得轻而易举。下面的这个例子实现了多文件上传功能。可以动态添加输入表单,上传的文件数量没有限制。代码如下:

multiupload.aspx

<%@ page language="vb" autoeventwireup="false" codebehind="multiupload.aspx.vb"
 inherits="aspxweb.multiupload" %>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
  <head>
    <title>多文件上传</title>
    <script language="javascript">
    function addfile()
    {
     var str = &<input type="file" size="50" name="file">&
     document.getelementbyid(&myfile&).insertadjacenthtml("beforeend",str)
    }
    </script>
  </head>
  <body>
    <form id="form1" method="post" runat="server" enctype="multipart/form-data">
      <center>
        <asp:label runat="server" id="mytitle"></asp:label>
        <p id="myfile"><input type="file" size="50" name="file"></p>
        <p>
          <input type="button" value="增加(add)" onclick="addfile()">
          <asp:button runat="server" text="上传" id="upload"></asp:button>
          <input onclick="this.form.reset()" type="button" value="重置(reset)">
        </p>
      </center>
      <p align="center">
        <asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true"
         font-size="9pt" width="500px" borderstyle="none" bordercolor="white"></asp:label>
      </p>
    </form>
  </body>
</html>

后代码:multiupload.aspx.vb

public class multiupload
    inherits system.web.ui.page
  protected withevents upload as system.web.ui.webcontrols.button
  protected withevents mytitle as system.web.ui.webcontrols.label
  protected withevents strstatus as system.web.ui.webcontrols.label

#region " web form designer generated code "

  &this call is required by the web form designer.
  <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

  end sub

  private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
    &codegen: this method call is required by the web form designer
    &do not modify it using the code editor.
    initializecomponent()
  end sub

#end region

  private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
    mytitle.text = "<h3>多文件上传</h3>"
    upload.text = "开始上传"
    if (me.ispostback) then me.saveimages()
  end sub

  private function saveimages() as system.boolean
    &遍历file表单元素
    dim files as system.web.httpfilecollection = system.web.httpcontext.current.request.files

    &状态信息
    dim strmsg as new system.text.stringbuilder("上传的文件分别是:<hr color=red>")
    dim ifile as system.int32
    try
      for ifile = 0 to files.count - 1
        &检查文件扩展名字
        dim postedfile as system.web.httppostedfile = files(ifile)
        dim filename, fileextension as system.string
        filename = system.io.path.getfilename(postedfile.filename)
        if not (filename = string.empty) then
          fileextension = system.io.path.getextension(filename)
          strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>")
          strmsg.append("客户端文件地址:" + postedfile.filename + "<br>")

本文关键:,在ASP.NET中实现多文件上传,
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top