近日需要做web文件管理系统,其中主要的一块就是文件上传,从网上收集了一些代码,经过修改后,基本上能满足当前项目要求。还有许多不完善的地方,希望各位高人有时间指点一二,特别时进度条的制作。代码内容如下:
aspx页面:
- <div id="MyFileShow">
- <table class="style10">
- <tr valign="top">
- <td class="style12" align="right" >
- <img alt="增加文件" src="../images/addfile.gif" οnclick="addFile()"/>
- </td>
- <td style="width:200;" align="right">
- <img alt="关闭" src="../images/colse.gif" οnclick="MyFileShow.style.display='none'"/>
- </td>
- </tr>
- <tr valign="top" style="height:140px;">
- <td class="style12" style="border:solid 1px #E0EEE0;" align="right">
- <div id="MyFile" style="list-style-type: none; margin-right:15px">
- <li>
- <input id="File1" type="file" name="File" runat="server" style="width: 297px" οnfοcus="showadddiv(this)" title="txt"/>
- <input name="text" type="hidden" id="txt" style="width:1px" />
- <input name="key" type="hidden" id="Hidtxt" style="width:1px" />
- </li>
- </div>
- <img src="../images/delfile.gif" οnclick="delFile()" alt="取消新增" style="display:none" id="img_del"/>
- </td>
- <td style="border:solid 1px #E0EEE0; border-left-width:0px;">
- <asp:Label ID="LabelKey" runat="server" Text="标签"></asp:Label>
- <br />
- <input id="Text_key" type="text" οnblur="add_key(this)" style="width:190px;"/><br />
- <asp:Label ID="LabelDescrip" runat="server" Text="描述"></asp:Label>
- <br />
- <textarea id="TextAreaadd" cols="20" rows="5" οnblur="add_onclick(this)" style="border:0px; width:190px;"></textarea>
- </td>
- </tr>
- <tr>
- <td class="style12" align="right">
- <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label>
- </td>
- <td align="left">
- <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" Width="50px" OnClientClick="loading()"/>
- </td>
- </tr>
- </table>
- </div>
- 本来想做一个上传进度条的,但是时间和水平有限,只能用个假的代替了,呵呵,往各位高人指点指点,不胜感激!
- <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%">
- <center>
- <br />
- <br />
- <img src="../images/loading.gif" alt=""/>
- <p>文件正在上传...</p>
- </center>
- <br />
- <center>
- <asp:Button ID="canceloading" runat="server" Text="终止" /> </center>
- <br />
- </div>
- js部分
-
- var i=1;
- function addFile()
- {
- var objArr=document.getElementById('MyFile').getElementsByTagName("li");
- var img_del=document.getElementById("img_del");
- if (objArr.length<6)
- { 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>';
- document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str);
- if(objArr.length>1)
- {
- img_del.style.display="block";
- }
- else if(objArr.length<2)
- {
- img_del.style.display="none";
- }
- else
- {
- return;
- }
- }
- else
- {
- alert("您一次最多只能上传6个文件!");
- }
- i++;
- }
- function delFile()
- {
- var objArr=document.getElementById('MyFile').getElementsByTagName("li");
- if(objArr.length>1)
- {
- objArr[objArr.length-1].removeNode(true);
- }
- }
- //给上传文件添加描述信息和搜索关键字
- function showadddiv(input)
- {
- var addvalue=document.getElementById("TextAreaadd");
- var addkey=document.getElementById("Text_key");
- addvalue.name=input.title;
- addkey.name="Hid"+input.title;
- }
- function add_onclick(add)
- {
- if(add.name!=null){
- var addinput=document.getElementById(add.name);
- if(add.value!="")
- {
- addinput.value=add.value;
- }
- }
- else
- {
- return false;
- }
- }
- function add_key(addkey)
- {
- if(addkey.name!=null){
- var addinputkey=document.getElementById(addkey.name);
- if(addkey.value!="")
- {
- addinputkey.value=addkey.value;
- }
- }
- else
- {
- return false;
- }
- }
- cs部分
-
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- lblMessage.Text = "";
- lblMessage.Visible = false;
- HttpFileCollection files = HttpContext.Current.Request.Files;
- //文件限制信息
- StringBuilder strmsg = new StringBuilder("");
- string[] Description = Request.Form["text"].Split(',');//文件描述
- string[] Key = Request.Form["key"].Split(',');//文件关键字
- int ifile;
- //文件大小及类型限制(还没确定具体限制类型和大小)
- for (ifile = 0; ifile < files.Count; ifile++)
- {
- if (files[ifile].FileName.Length > 0)
- {
- HttpPostedFile postedfile = files[ifile];
- }
- }
- if (strmsg.Length <= 0)
- {
- //以下为创建文件目录(不存在则创建)
- string dirname ="test";
- string dirpath = Server.MapPath("../") + "upload";
- dirpath = dirpath + @"/" + dirname;
- if (Directory.Exists(dirpath) == false)
- {
- Directory.CreateDirectory(dirpath);
- }
- string ppath = "";
- string FilePath = "";
- int filesize = 0;
- string FileExtention = "";
- string FileName = "";
- long groupid = Convert.ToInt32(ViewState["GroupID"]);
- for (int i = 0; i < files.Count; i++)
- {
- HttpPostedFile myFile = files[i];
- FileName = Path.GetFileName(myFile.FileName);
- if (myFile.ContentLength>0)//有文件才执行上传操作
- {
- FileExtention = System.IO.Path.GetExtension(myFile.FileName);
- ppath = dirpath + @"/" + FileName;
- FilePath = "upload/" + lu.UID + "/" + FileName;
- filesize = myFile.ContentLength;
- myFile.SaveAs(ppath);
- //以下为写入数据库方法(省去)
- Response.Write(FileName + "," + Description[i] + "," + Key[i]);
- }
- //写入数据库,判断文件是否存在
- }
- }
- else
- {
- lblMessage.Text = strmsg.ToString();
- lblMessage.Visible = true;
- }
- }