using System;
using System.Data;
using System.Configuration;
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;public partial class _uploadIT : System.Web.UI.Page
{
    //protected System.Web.UI.WebControls.Button UploadButton;
    // protected System.Web.UI.WebControls.Label strStatus;    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["adminName"].ToString().Trim() != "")
        {
                if((Convert.ToInt32(Session["level"].ToString()) == 3 && Convert.ToInt32(Session["class"].ToString()) == 1)|| (Convert.ToInt32(Session["level"].ToString()) == 5))
                //(Convert.ToInt32(Session["level"].ToString()) >= 4)
                {
                    
                }
                else
                {
                    Response.Redirect("error.html");
                }        }
        else
        {
              Response.Redirect("login.aspx");
        }
             }    private Boolean SaveImages()
    {        HttpFileCollection files = HttpContext.Current.Request.Files;
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        strMsg.Append("上传的文件分别是:<hr color=red>");
        try
        {
            for (int iFile = 0; iFile < files.Count; iFile++)
            {
                HttpPostedFile postedFile = files[iFile];
                string fileName, fileExtension, file_id;
                int year = System.DateTime.Now.Year;
                int month = System.DateTime.Now.Month;
                int day = System.DateTime.Now.Day;
                int hour = System.DateTime.Now.Hour;
                int minute = System.DateTime.Now.Minute;
                int second = System.DateTime.Now.Second;
                int millisecond = System.DateTime.Now.Millisecond;
                //string my_file_id = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millisecond.ToString() + iFile.ToString();                fileName = System.IO.Path.GetFileName(postedFile.FileName);
                fileExtension = System.IO.Path.GetExtension(fileName);
                file_id = fileName;
                if (fileName != "")
                {
                    fileExtension = System.IO.Path.GetExtension(fileName);
                    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名:" + file_id + "<br>");
                    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
                    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("upload/") + file_id);
                }
            }
            strStatus.Text = strMsg.ToString();
            return true;
        }
        catch (System.Exception Ex)
        {
            strStatus.Text = Ex.Message;
            return false;
        }
    }    protected void UploadButton_Click(object sender, EventArgs e)
    {
        Boolean bl = SaveImages();
        if (bl)
        {
            adminDataBase db = new adminDataBase();
            int l = db.Log(Session["adminName"].ToString(), "上传文件");
            Response.Write("<script>alert('上传成功!')</script>");
        }
        else
        {
            Response.Write("<script>alert('上传失败!')</script>");
        }
    }
}

解决方案 »

  1.   

    <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="uploadIT.aspx.cs" Inherits="_uploadIT" %><!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>
         <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">
           <div align="center">
             <h3>多文件上传</h3>
             <P id="MyFile"><input type="file" size="50" NAME="File"></P>
             <P>
               <input type="button" value="增加(Add)" onclick="addFile()">
               <input onclick="this.form.reset()" type="button" value="重置(ReSet)">
               <asp:Button Runat="server" Text="开始上传" ID="UploadButton" OnClick="UploadButton_Click"></asp:Button>
             </P>
             <P>
             <asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt" 
               Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
             </P>
           </div>
         </form>
       </body>
    </html>
      

  2.   

    写的不错 可是我的文件路径是从Excel文件中读出来的,根本不用FileUpload控件,这该怎么做呢?
    我期待有人能帮我解决一下啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      

  3.   

    .NET联盟会馆 QQ群号:77329563   招聘中.....
      

  4.   

    自己找到一个用WebClient的例子 希望可以实现。。
      

  5.   

    唉,WebClient老出错~~~~ 上帝啊 怎么实现啊~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~顶!!!!!!!!!!!!
      

  6.   

    没有FileUpload
    考虑ftp和activex方式上传.
      

  7.   

    那里应该是将文件保存在什么位置..FileUpload1.PostedFile.SaveAs(path+FileUpload1.FileName);//将文件保存在根目录下