http://blog.sina.com.cn/itmsn[订阅]
字体大小: 正文
上传多个文件实例(net)(2007-10-15 17:11:25)
 《1》

<%@ Import Namespace="System.IO" %>
<%@ page Language="C#" debug="true" %>
<html>

<head>
<title>文件上传的实例</title>

<script language="C#" runat="server">
    //This method is called when the "upload" button id pressed
    public void UploadFile(object sender , EventArgs E) {
        HttpFileCollection myFile  = HttpContext.Current.Request.Files;
        for(int i=0; i<myFile.Count; i++) {
            HttpPostedFile postedFile = myFile[i];
            
            if(postedFile.ContentLength != 0) {
                string fileName = Path.GetFileName(postedFile.FileName);
                
                DateTime now = DateTime.Now;
                string nowtime=now.Year.ToString()+now.Month.ToString()+now.Day.ToString()+now.Hour.ToString()+now.Minute.ToString()+now.Second.ToString();
                
                //获得文件名扩展
                string fileExtension = System.IO.Path.GetExtension(fileName);
                
                //保存文件到你所要的目录,这里是IIS根目录下的upfiles目录.你可以改变. 
                //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
                 postedFile.SaveAs(Server.MapPath("~\\upfiles\\"+nowtime+fileName));
                //postedFile.SaveAs(Request.MapPath("upFiles/") +nowtime+fileName);
                
                //得到这个文件的相关属性:文件名,文件类型,文件大小
                fname.Text=postedFile.FileName;
                fenc.Text=postedFile.ContentType+"<br>"+fileExtension;
                fsize.Text=postedFile.ContentLength.ToString()+"<br>"+nowtime.ToString();
            }
        }
    }
</script>

</head>

<body>

<h3 align="center">文件上传的实例 </h3>
<div align="center">
    <table border="1" cellspacing="2" cellpadding="2">
        <form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data" runat="server">
            <tr>
                <td><h5 align="center">选择要上传的文件:</h5></td>
            </tr>
            <tr>
                <td><input type="file" id="myFile" name="myFile"></td>
            </tr>
            <tr>
                <td><input type="file" id="File1" name="myFile"></td>
            </tr>
            <tr>
                <td><input type="file" id="File2" name="myFile"></td>
            </tr>
            <tr>
                <td><input type="button" value="上 传" onserverclick="UploadFile" runat="server" id="Button1" name="Button1"></td>
            </tr>
        </form>
    </table>
</div>
<br>
<br>
<div align="center">
    <table border="1" cellspacing="2">
        <tr>
            <td><b>文件资料</b></td>
            <td> </td>
        </tr>
        <tr>
            <td>文件名</td>
            <td><asp:Label ID="fname" Text="" runat="server" /></td>
        </tr>
        <tr>
            <td>文件类型</td>
            <td><asp:Label ID="fenc" runat="server" /></td>
        </tr>
        <tr>
            <td>文件大小(in bytes)</td>
            <td><asp:Label ID="fsize" runat="server" /></td>
        </tr>
    </table>
</div>

</body>

</html>

 

<2>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="upFiles_Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server" Height="171px" Width="356px">
            <asp:FileUpload ID="FileUpload1" runat="server" Width="273px" /><br />
        </asp:Panel>
   
    </div>
        <asp:TextBox ID="TextBox1" runat="server" Width="93px"></asp:TextBox>&nbsp;<asp:Button
            ID="Button1" runat="server" OnClick="Button1_Click" Text="增加" />
        <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="上传" />
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class upFiles_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        FileUpload fu;

        for (int i = 0; i < Convert.ToInt32(TextBox1.Text); i++)
        {
            fu = new FileUpload();
            fu.ID = "fu" + i.ToString();
            Panel1.Controls.Add(fu);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string f_name, f_size, f_type;//声明变量

        HttpFileCollection hfc = Request.Files;
        //获取客户端文件集合

        for (int i = 0; i < hfc.Count; i++)
        {
          //循环集合
            HttpPostedFile hpf= hfc[i];
            f_name = Path.GetFileName(hpf.FileName);
            f_size = hpf.ContentLength.ToString();
            f_type = hpf.ContentType;
            hpf.SaveAs(Server.MapPath("~/up/")+f_name);
            Response.Write(f_name+"<br>"+f_size+"<br>"+f_type+"<br>");
        }

    }
}

加载中,请稍候...
  • 评论加载中,请稍候...

验证码:请点击后输入验证码  收听验证码

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

相关博文
读取中...
推荐博文
读取中...