. net多个文件上传

近日需要做web文件管理系统,其中主要的一块就是文件上传,从网上收集了一些代码,经过修改后,基本上能满足当前项目要求。还有许多不完善的地方,希望各位高人有时间指点一二,特别时进度条的制作。代码内容如下:

aspx页面:

  1.   <div id="MyFileShow">
  2.                    <table class="style10">
  3.                        <tr valign="top">
  4.                            <td class="style12" align="right" >
  5.                                <img alt="增加文件" src="../images/addfile.gif" οnclick="addFile()"/>
  6.                                </td>
  7.                            <td style="width:200;" align="right">
  8.                                <img alt="关闭" src="../images/colse.gif" οnclick="MyFileShow.style.display='none'"/>
  9.                                </td>
  10.                        </tr>
  11.                        <tr valign="top" style="height:140px;">
  12.                            <td class="style12"  style="border:solid 1px #E0EEE0;" align="right">
  13.                            <div id="MyFile" style="list-style-type: none; margin-right:15px">
  14.                            <li>
  15.                            <input id="File1" type="file" name="File" runat="server" style="width: 297px" οnfοcus="showadddiv(this)" title="txt"/>
  16.                            <input name="text" type="hidden" id="txt"  style="width:1px" />
  17.                            <input name="key" type="hidden"  id="Hidtxt" style="width:1px" />
  18.                            </li>
  19.                            </div>
  20.                            <img  src="../images/delfile.gif" οnclick="delFile()" alt="取消新增" style="display:none" id="img_del"/>
  21.                            </td>
  22.                            <td style="border:solid 1px #E0EEE0; border-left-width:0px;">
  23.                                <asp:Label ID="LabelKey" runat="server" Text="标签"></asp:Label>
  24.                                <br />
  25.                                <input id="Text_key" type="text" οnblur="add_key(this)"  style="width:190px;"/><br />
  26.                                <asp:Label ID="LabelDescrip" runat="server" Text="描述"></asp:Label>
  27.                                <br />
  28.                                 <textarea id="TextAreaadd" cols="20" rows="5" οnblur="add_onclick(this)" style="border:0px; width:190px;"></textarea>  
  29.                          </td>
  30.                        </tr>
  31.                        <tr>
  32.                            <td class="style12"  align="right">
  33.           <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>  
  34.                            </td>
  35.                            <td align="left">
  36.                                <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click"   Width="50px" OnClientClick="loading()"/> 
  37.                            </td>
  38.                        </tr>
  39.                    </table>
  40.                </div>
  41. 本来想做一个上传进度条的,但是时间和水平有限,只能用个假的代替了,呵呵,往各位高人指点指点,不胜感激!
  42.                <div id="loading" style=" border:#333333 1px dashed;font-size: 9pt; display:none;  width: 200px; color: #000000; border-bottom: #333333 1px dashed; position: absolute; height: 80px; background-color: #ffffff; z-index:10;left:450px; top:55%">
  43.                         <center>
  44.                             <br />
  45.                             <br />
  46.                             <img  src="../images/loading.gif" alt=""/>
  47.                             <p>文件正在上传...</p>
  48.                             </center>
  49.                         <br />
  50.                         <center>
  51.                             <asp:Button ID="canceloading" runat="server" Text="终止" /> </center>
  52.                         <br />
  53.                     </div>
  54. js部分
    1. var i=1;
    2.    function addFile() 
    3.    { 
    4.    var objArr=document.getElementById('MyFile').getElementsByTagName("li");
    5.    var img_del=document.getElementById("img_del");
    6.    if (objArr.length<6) 
    7.    { var str = '<li><input name="text" type="hidden"  id="txt'+i+'" style="width:1px"/><input name="key" type="hidden"  id="Hidtxt'+i+'" style="width:1px"/><input type="file" name="File" runat="server" title="txt'+i+'" οnfοcus="showadddiv(this)" style="width:297px"/></li>';
    8.      document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
    9.      if(objArr.length>1)
    10.      {
    11.        img_del.style.display="block";
    12.      }
    13.      else if(objArr.length<2)
    14.      {
    15.         img_del.style.display="none";
    16.      }
    17.      else
    18.      {
    19.       return;
    20.      }
    21.    } 
    22.    else 
    23.    { 
    24.      alert("您一次最多只能上传6个文件!"); 
    25.    } 
    26.    i++;
    27.    } 
    28.    function delFile()
    29.    {
    30.    var objArr=document.getElementById('MyFile').getElementsByTagName("li");
    31.    if(objArr.length>1)
    32.    {
    33.     objArr[objArr.length-1].removeNode(true);
    34.    }
    35.    }
    36.  //给上传文件添加描述信息和搜索关键字
    37. function showadddiv(input)
    38. {
    39.     var addvalue=document.getElementById("TextAreaadd");
    40.     var addkey=document.getElementById("Text_key");
    41.     addvalue.name=input.title;
    42.     addkey.name="Hid"+input.title;
    43. }
    44. function add_onclick(add) 
    45. {
    46.      if(add.name!=null){
    47.      var addinput=document.getElementById(add.name);
    48.      if(add.value!="")
    49.      {
    50.      addinput.value=add.value;
    51.      }
    52.      }
    53.      else
    54.      {
    55.       return false;
    56.      }
    57. }
    58. function add_key(addkey) 
    59. {
    60.       if(addkey.name!=null){
    61.      var addinputkey=document.getElementById(addkey.name);
    62.      if(addkey.value!="")
    63.      {
    64.        addinputkey.value=addkey.value;
    65.      }
    66.      }
    67.       else
    68.      {
    69.       return false;
    70.      }
    71. }
  55. cs部分
    1.  protected void btnUpload_Click(object sender, EventArgs e)
    2.         {
    3.             lblMessage.Text = "";
    4.             lblMessage.Visible = false;
    5.             HttpFileCollection files = HttpContext.Current.Request.Files;
    6.             //文件限制信息
    7.             StringBuilder strmsg = new StringBuilder("");
    8.             string[] Description = Request.Form["text"].Split(',');//文件描述 
    9.             string[] Key = Request.Form["key"].Split(',');//文件关键字
    10.             int ifile;
    11.             //文件大小及类型限制(还没确定具体限制类型和大小)
    12.             for (ifile = 0; ifile < files.Count; ifile++)
    13.             {
    14.                 if (files[ifile].FileName.Length > 0)
    15.                 {
    16.                     HttpPostedFile postedfile = files[ifile];
    17.                 }
    18.             }
    19.             if (strmsg.Length <= 0)
    20.             {
    21.                 //以下为创建文件目录(不存在则创建) 
    22.                 string dirname ="test";
    23.                 string dirpath = Server.MapPath("../") + "upload";
    24.                 dirpath = dirpath + @"/" + dirname;
    25.                 if (Directory.Exists(dirpath) == false)
    26.                 {
    27.                     Directory.CreateDirectory(dirpath);
    28.                 }
    29.                 string ppath = "";
    30.                 string FilePath = "";
    31.                 int filesize = 0;
    32.                 string FileExtention = "";
    33.                 string FileName = "";
    34.                 long groupid = Convert.ToInt32(ViewState["GroupID"]);
    35.                 for (int i = 0; i < files.Count; i++)
    36.                 {
    37.                     HttpPostedFile myFile = files[i];
    38.                     FileName = Path.GetFileName(myFile.FileName);
    39.                     if (myFile.ContentLength>0)//有文件才执行上传操作 
    40.                     {
    41.                         FileExtention = System.IO.Path.GetExtension(myFile.FileName);
    42.                         ppath = dirpath + @"/" + FileName;
    43.                         FilePath = "upload/" + lu.UID + "/" + FileName;
    44.                         filesize = myFile.ContentLength;
    45.                         myFile.SaveAs(ppath);
    46.                     //以下为写入数据库方法(省去)
    47.                         Response.Write(FileName + "," + Description[i] + "," + Key[i]);
    48.                     }
    49.                     //写入数据库,判断文件是否存在 
    50.                 }
    51.                 
    52.             }
    53.             else
    54.             {
    55.                 lblMessage.Text = strmsg.ToString();
    56.                 lblMessage.Visible = true;
    57.             }
    58.         }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值