各位高手们快帮忙啊,怎样实现 

解决方案 »

  1.   

    这个是最基础的上传吧...可以参考http://download.csdn.net/source/1050100另:附个例子参考%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUP.aspx.cs" Inherits="FileUP" %><!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>
    <script language="JavaScript" type="text/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"></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>
    ///*******************************cs代码*************************************///using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;public partial class FileUP : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    /// 在此处放置用户代码以初始化页面
    if (this.IsPostBack) this.SaveImages();
    }private Boolean SaveImages()
    {
    ///'遍历File表单元素
    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;
    fileName = System.IO.Path.GetFileName(postedFile.FileName);
    if (fileName != "")
    {
    fileExtension = System.IO.Path.GetExtension(fileName);
    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
    strMsg.Append("上传文件的文件名:" + fileName + "<br>");
    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
    ///'可根据扩展名字的不同保存到不同的文件夹
    ///注意:可能要修改你的文件夹的匿名写入权限。
    postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFiles/") + fileName);
    }
    }
    strStatus.Text = strMsg.ToString();
    return true;
    }
    catch(System.Exception Ex)
    {
    strStatus.Text = Ex.Message;
    return false;
    }
    }
      

  2.   

    无组件上传?
    namespace ShowBo.AspNetUpLoad
    {
        using System;
        using System.IO;
        using System.Web;
        using System.Text;
        using System.Collections;
        /// <summary>
        /// 文件结构
        /// </summary>
        public struct MyFileInfo
        {
            public string FileName;//服务器端的文件名
            public string SFileName;//客户端的文件名
            public byte[] FileData;//文件的2进制数据
        }
        /// <summary>
        /// 表单项收集
        /// </summary>
        public class FormCollection : DictionaryBase
        {
            /// <summary>
            /// 添加项目
            /// </summary>
            /// <param name="Key">键</param>
            /// <param name="Value">值</param>
            public void Add(string Key, string Value)
            {
                Dictionary.Add(Key, Value);
            }
            /// <summary>
            /// 获取或设置对应的键值
            /// </summary>
            /// <param name="Key">键</param>
            /// <returns>值</returns>
            public string this[string Key]
            {
                get { return (string)Dictionary[Key]; }
                set { Dictionary[Key] = value; }
            }
            /// <summary>
            /// 重写叠代借口返回,返回string类型
            /// </summary>
            /// <returns>IEnumerator叠代接口</returns>
            public new IEnumerator GetEnumerator()
            {
                foreach (DictionaryEntry de in Dictionary)
                {
                    yield return (string)de.Value;
                }
            }
            /// <summary>
            /// 删除键值
            /// </summary>
            /// <param name="Key">要删除的键值</param>
            public void Remove(string Key)
            {
                Dictionary.Remove(Key);
            }
        }
        /// <summary>
        /// 上传文件收集,信息为2进制
        /// </summary>
        public class FileCollection : DictionaryBase
        {   
            public void Add(string Key, MyFileInfo fileInfo)
            {
                Dictionary.Add(Key, fileInfo);
            }
            public void Remove(string Key)
            {
                Dictionary.Remove(Key);
            }
            /// <summary>
            /// 重写迭代接口接口
            /// </summary>
            /// <returns>返回文件的MyFileInfo结构体</returns>
            public new  IEnumerator GetEnumerator()
            {
                foreach (DictionaryEntry de in Dictionary)
                {
                    yield return (MyFileInfo)de.Value;
                }
            }       
            public MyFileInfo this[string Key]
            {
                get { return (MyFileInfo)Dictionary[Key]; }
                set { Dictionary[Key] = value; }
            }       
        }
        /// <summary>
        /// 文件上传类
        /// </summary>
        public class UpLoad
        {
            #region 变量申明
            private byte[] FormData; //表单的2进制数据数组
            private int formSize;//总上传字节数
            private string allowExts;//默认的可以上传的文件扩展名称
            private string rootPath;//文件保存的目录
            private string fileErr;//文件错误信息,例如文件大小超过,扩展名不允许
            private string allFile;//保存所有成功上传的文件名称
            
            private int maxFileSize;//默认上传大小为200kb
            private int fileCount;//总的上传的文件个数
            private byte[] SplitStr;//2进制分隔符号
            private int SplitStrLen;//分隔符号长度
            byte[] CLStr = new byte[] { 13, 10 };//2进制回车
            private FileCollection fileCollections = new FileCollection();//文件信息集合
            private FormCollection formCollections = new FormCollection();//表单信息集合
            private BinaryReader binReader;//2进制读取器
            #endregion
            #region 属性
            /// <summary>
            /// 默认的可以上传的文件扩展名称
            /// </summary>
            public string AllowExts
            {
                get { return allowExts; }
                set { allowExts = value; }
            }
            /// <summary>
            /// 文件保存的目录
            /// </summary>
            public string RootPath
            {
                get { return rootPath; }
                set 
                {
                    string tempPath = value;
                    if (tempPath.LastIndexOf("/") == -1)
                        rootPath = tempPath + "/";
                    else
                        rootPath = tempPath;
                }
            }     
            /// <summary>
            /// 错误信息,只读属性
            /// </summary>
            public string ErrorStr
            {
                get
                {
                    if (fileErr != "")
                        fileErr = "发生如下错误\n" + fileErr;
                    return fileErr;
                }
            }
            /// <summary>
            /// 文件信息集合,2进制流形式缓存数据,只读属性
            /// </summary>
            public FileCollection FileCollections
            {
                get { return fileCollections; }
            }
            /// <summary>
            /// 表单信息集合,只读属性
            /// </summary>
            public FormCollection FormCollections
            {
                get { return formCollections; }
            }
            /// <summary>
            /// 所有文件在服务器上生成的文件名,只读属性
            /// </summary>
            public string FileNames
            {
                get
                {
                    return allFile;
                }
            }
            /// <summary>
            /// 文件数量,只读属性
            /// </summary>
            public int FileCount
            {
                get { return fileCount; }
            }
            #endregion        
           
            public UpLoad()
            {
                allowExts = "jpg|png|gif";
                rootPath = "uploadfiles/";
                maxFileSize = 200;
                allFile = "";
                fileErr = "";
                fileCount = 0;
            }       
            /// <summary>
            /// 保存文件到硬盘
            /// </summary>
            public void Save()
            {
                string FilePath = "";
                FileStream fileStream;
                foreach (MyFileInfo mfi in fileCollections)
                {
                    try
                    {
                        FilePath = HttpContext.Current.Server.MapPath(rootPath + mfi.FileName);//获取文件的物理路径
                        fileStream = new FileStream(FilePath, FileMode.OpenOrCreate);
                        fileStream.Write(mfi.FileData, 0, mfi.FileData.Length);
                        fileStream.Flush();
                        fileStream.Close();
                    }
                    catch
                    {
                        fileErr += mfi.SFileName+"保存失败!\n";
                    }
                }
            }
            /// <summary>
            /// 以时间和文件第几个为文件重新命名
            /// </summary>
            /// <param name="Ext">扩展名</param>
            /// <param name="fileCount">第几个文件,防止服务器处理速度快,一个时间内能处理几个文件,那么用时间生成的文件名有可能被覆盖。</param>
            /// <returns>新文件名</returns>
            private string getFileName(string Ext, int fileCount)
            {
                return DateTime.Now.ToString().Replace(":", "").Replace(" ", "").Replace("-", "") + fileCount.ToString() + "." + Ext;
            }
            /// <summary>
            /// 获取表单数据项目
            /// </summary>
            public void CollectFormDatas()
            {
                string sfilename = "", fileExt = "",FileName="";
                string Key = "";//表单键名称
                int DataSize, headstart, headend;
                formSize = HttpContext.Current.Request.TotalBytes;
                if (formSize < 1)
                {
                    ErrHandle("没有选择上传的文件!");
                }
                FormData = new byte[formSize];
                binReader = new BinaryReader(HttpContext.Current.Request.InputStream);
                binReader.Read(FormData, 0, formSize);
                SplitStrLen = InStrB(0, FormData, CLStr);
                SplitStr = new byte[SplitStrLen];
                binReader.BaseStream.Position = 0;//设置流位置
                binReader.Read(SplitStr, 0, SplitStrLen);//获取2进制分隔符号
                headstart = SplitStrLen + 2;//第一个集合的描述信息位置                      
                do
                {
                    headend = InStrB(headstart, FormData, new byte[] { CLStr[0], CLStr[1], CLStr[0], CLStr[1] });//两对回车换行符 
                    DataSize = InStrB(headend + 4, FormData, SplitStr) - (headend + 4)-2;
                    fileExt = getExt(headstart, headend - headstart , ref sfilename, ref Key);
                    if (fileExt == null)//表单集合
                    {
                        formCollections.Add(Key, ReadData(headend+4, DataSize));
                    }
                    else if (fileExt != "")//文件
                    {
                        
                        if (!checkFileSize(DataSize))
                            fileErr = fileErr + sfilename + "上传失败,文件大小超过" + maxFileSize + "KB!\n";
                        else if (!checkExt(fileExt))
                            fileErr = fileErr + sfilename + "上传失败,文件类型 " + fileExt + " 不允许上传!\n";
                        else
                        {
                            fileCount = fileCount + 1;//累计文件个数
                            FileName = getFileName(fileExt, fileCount);                        
                            ReadData(headend+4, DataSize, Key,FileName,sfilename);
                        }
                    }
                    //计算下项开始位置
                    headstart = (headend+4) + DataSize +2+ (SplitStrLen + 2);
                } while (headstart + 2 < formSize);
                binReader.Close();  //关闭流 
            }
      

  3.   


            /// <summary>
            /// 错误处理
            /// </summary>
            /// <param name="ErrMsg">发生的错误</param>
            private void ErrHandle(string ErrMsg)
            {
                ErrMsg = ErrMsg.Replace("'", "\'");//防止脚本符号的嵌套出错
                HttpContext.Current.Response.Write("<script>alert('" + ErrMsg + "');history.back();</" + "script>");
                HttpContext.Current.Response.End();
            }
    private int InStrB(int startPosition, byte[] b, byte[] sb)
            {
                int blen = b.Length;
                if (startPosition >= blen)
                    return -1;
                int sblen = sb.Length;
                int postion = -1;
                if (sblen == 0)
                    return -1;
                for (int i = startPosition; i < blen; i++)
                {
                    if (b == sb[0])
                    {
                        int TempI = i + 1;
                        bool Find = true; ;
                        for (int j = 1; j < sblen && TempI < blen; j++, TempI++)
                        {
                            if (b[TempI] == sb[j])
                                continue;
                            else
                            {
                                Find = false;
                                break;
                            }
                        }
                        if (Find)
                        {
                            postion = i;
                            break;
                        }
                    }
                }
                return postion;
            }
        }
    }
    测试页面
    <!!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>asp.net无组件上传</title>
        <meta http-equiv="content-type" content='text/html;utf-8' />
    </head>
    <body>
    <script type="text/javascript">
    function check(f)
    {
       var choose=false;
       //至少要选择一个文件
       for(var i=0;i<10;i++)
       {
         if(eval("f.myfile"+i)!=undefined)
         {
             if(eval("f.myfile"+i).value!="")
             {
               choose=true;
               break;
             }
          }
       }
       if(!choose)
       {
           alert('请选择上传的文件!');
           f.myfile0.focus();
           return false;
       }
    }
    function SetFile()
    {
      var fc=document.getElementById('filecount');
      if(fc.value=="")
      {
        alert('文件个数不能为空!');
        fc.focus();
        return ;
      }
      if(isNaN(fc.value))
      {
        alert('文件个数为整数!');
        fc.select();
        return;
      }
      var count=parseInt(fc.value);
      if(count<1||count>10)
      {
        alert('一次上传文件个数为1-10个!');
        fc.select();
        return false;
      }
      var div=document.getElementById('divFile');
      div.innerHTML="";
      for(var i=0;i  {
         div.innerHTML+='请选择文件:<input type="file" name="myfile'+i+'" style="width:200px" /><br/>';
      }
    }
    </script>  
    <form method="post" enctype='multipart/form-data' action="getfile.aspx" >
     文件个数:<input type="text" size='2' name='filecount' id='filecount' />(1-10个)<input type="button" onclick='SetFile()' value="设置上传个数"/>
    <div id='divFile'>请选择文件:<input type="file" name="myfile0" style="width:200px" /></div>
    <input type="submit" value='开始上传' />
    </form>
    </body>
    </html>接收页面
    <%@ Page Language="C#" %>
    <%@ Import Namespace="ShowBo.AspNetUpLoad" %>
    <script runat="server"> 
        protected void Page_Load(object sender, EventArgs e)
        {
            UpLoad upload = new UpLoad();
            upload.CollectFormDatas();
            upload.Save();
            string Err = upload.ErrorStr;
            Response.Write(upload.FormCollections["filecount"]);
            if (Err != "")
                Response.Write(Err);
            else
                Response.Write("Ok");   
        }
    </script> 
    ~~Kao~居然要发两次~