首页 > 编程 > .NET > 正文

Asp.net(C#)多文件上传

2024-07-10 13:00:04
字体:
来源:转载
供稿:网友

fileup.aspx 页面


 1<%@ page language="c#" codebehind="fileup.aspx.cs" autoeventwireup="false" inherits="testcenter.fileup" %>
 2<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
 3<html>
 4  <head>
 5    <title>多文件上传</title>
 6    <script language="javascript">
 7    function addfile()
 8    {
 9     var str = '<input type="file" size="50" name="file">'
10     document.getelementbyid('myfile').insertadjacenthtml("beforeend",str)
11    }
12    </script>
13  </head>
14  <body>
15    <form id="form1" method="post" runat="server" enctype="multipart/form-data">
16      <div align="center">
17        <h3>多文件上传</h3>
18        <p id="myfile"><input type="file" size="50" name="file"></p>
19        <p>
20          <input type="button" value="增加(add)" >
21          <input type="button" value="重置(reset)">
22          <asp:button runat="server" text="开始上传" id="uploadbutton"></asp:button>
23        </p>
24        <p>
25        <asp:label id="strstatus" runat="server" font-names="宋体" font-bold="true" font-size="9pt" 
26          width="500px" borderstyle="none" bordercolor="white"></asp:label>
27        </p> 
28      </div>
29    </form>
30  </body>
31</html>
32

 

//****************************************************************
//////fileup.aspx.cs


 1using system;
 2using system.collections;
 3using system.componentmodel;
 4using system.data;
 5using system.drawing;
 6using system.web;
 7using system.web.sessionstate;
 8using system.web.ui;
 9using system.web.ui.webcontrols;
10using system.web.ui.htmlcontrols;
11
12namespace testcenter
13{
14  /**//// <summary>  
15  /// 实现多文件上传
16  /// </summary>
17  public class fileup: system.web.ui.page
18  {
19    protected system.web.ui.webcontrols.button uploadbutton;
20    protected system.web.ui.webcontrols.label strstatus;
21
22    private void page_load(object sender, system.eventargs e)
23    {
24            if (this.ispostback) this.saveimages();
25    }
26
27    private boolean saveimages()
28    {
29      /**////'遍历file表单元素
30      httpfilecollection files  = httpcontext.current.request.files;
31
32      /**//// '状态信息
33      system.text.stringbuilder strmsg = new system.text.stringbuilder();
34      strmsg.append("上传的文件分别是:<hr color=red>");
35      try
36      {
37        for(int ifile = 0; ifile < files.count; ifile++)
38        {
39          /**////'检查文件扩展名字
40          httppostedfile postedfile = files[ifile];
41          string filename, fileextension;
42          filename = system.io.path.getfilename(postedfile.filename);
43          if (filename != "")
44          {
45            fileextension = system.io.path.getextension(filename);
46            strmsg.append("上传的文件类型:" + postedfile.contenttype.tostring() + "<br>");
47            strmsg.append("客户端文件地址:" + postedfile.filename + "<br>");
48            strmsg.append("上传文件的文件名:" + filename + "<br>");
49            strmsg.append("上传文件的扩展名:" + fileextension + "<br><hr>");
50            /**////'可根据扩展名字的不同保存到不同的文件夹
51            ///注意:可能要修改你的文件夹的匿名写入权限。
52            postedfile.saveas(system.web.httpcontext.current.request.mappath("images/") + filename);
53          }
54        }
55        strstatus.text = strmsg.tostring();
56        return true;
57      }
58      catch(system.exception ex)
59      {
60        strstatus.text = ex.message;
61        return false;
62      }
63    }
64

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表