一次上传多个文件示例代码:
cs部分
- 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 fileupload : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnUpload_Click(object sender, EventArgs e)
- {
- lblMessage.Text = "";
- lblMessage.Visible = false;
- System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
- System.Text.StringBuilder strmsg = new System.Text.StringBuilder("");
- string[] rd = Request.Form[1].Split(',');//文件描述
- string albumid=ddlAlbum.SelectedValue.Trim();
- int ifile;
- for (ifile = 0; ifile < files.Count; ifile++)
- {
- if (files[ifile].FileName.Length > 0)
- {
- System.Web.HttpPostedFile postedfile = files[ifile];
- if (postedfile.ContentLength / 1024 > 1024)//单个文件不能大于1024k
- {
- strmsg.Append(Path.GetFileName(postedfile.FileName) + "---不能大于1024k<br>");
- break;
- }
- string fex = Path.GetExtension(postedfile.FileName);
- if (fex != ".jpg" && fex != ".rar" && fex != ".gif" && fex != ".doc")
- {
- strmsg.Append(Path.GetFileName(postedfile.FileName) + "---格式不对<br>");
- break;
- }
- }
- }
- if (strmsg.Length <= 0)
- {
- //以下为创建文件目录
- string dirname = "pic00" + ddlAlbum.SelectedValue.Trim();
- string dirpath = Server.MapPath("./") + "upload";
- dirpath = dirpath + @"/" + dirname;
- if (Directory.Exists(dirpath) == false)
- {
- Directory.CreateDirectory(dirpath);
- }
- Random ro = new Random();
- int name = 1;
- for (int i = 0; i < files.Count; i++)
- {
- System.Web.HttpPostedFile myFile = files[i];
- string FileName = "";
- string FileExtention = "";
- FileName = System.IO.Path.GetFileName(myFile.FileName);
- string stro=ro.Next(100,100000000).ToString()+name.ToString();//产生一个随机数用于新命名的文件
- string NewName =DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+stro;
- if (FileName.Length > 0)//有文件才执行上传操作
- {
- FileExtention = System.IO.Path.GetExtension(myFile.FileName);
- string ppath = dirpath + @"/" + FileName + FileExtention;
- myFile.SaveAs(ppath);
- string FJname = FileName;
- }
- Response.Write(rd[i].ToString());
- //写入数据库
- //AddPicture(PicPath, rd[i], albumid);
- }
- }
- else
- {
- lblMessage.Text = strmsg.ToString();
- lblMessage.Visible = true;
- }
- }
- }
- html部分
-
- <div style="font-size:9pt;">
- <br />
- <table >
- <tr valign="top">
- <td >
- 请选择文件:</td>
- <td ><div id="MyFile">
- <input id="File1" type="file" name="File" runat="server" style="width: 250px" onchange="uploadSelect($(this));"/> 描述:<input name="text" type="text" style="width: 150px" maxlength="20" /><br /></div></td>
- <td style="width: 100px">
- <input onclick="addFile()" type="button" value="增加文件"/></td>
- </tr>
- <tr>
- <td >
- 指定文件夹:</td>
- <td >
- <asp:DropDownList ID="ddlAlbum" runat="server" Width="98px">
- </asp:DropDownList>
- <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> </td>
- <td >
- <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red"></asp:Label></td>
- </tr>
- </table>
- </div>
- js部分
-
- <script type="text/javascript">
- var i=1
- function addFile()
- {
- if (i<6)
- {var str = ' <input type="file" name="File" runat="server" style="width: 250px"/>描述:<input name="text" type="text" style="width: 150px" maxlength="20" /><br/>'
- document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
- }
- else
- {
- alert("您一次最多只能上传6个文件!")
- }
- i++
- }
- var uploadSelect = function(el){
- el.fadeOut("show");
- parent.uploading(document.getElementById("<%=File1.ClientID %>").value);
- $("#<%=frmUpload.ClientID %>").submit();
- };
- </script>