首页 > 代码库 > Html上传多个文件

Html上传多个文件

1.页面

<span style="float: left;text-align:left;" id="spanFileList">                                 <input type="button" value="http://www.mamicode.com/添加附件" onclick="AddFiles()" style="margin-top: 5px; margin-left: 5px" /><br /> </span>

2.js

//添加附件    

    function AddFiles() {    

        var fileList = document.getElementById("spanFileList");

         var file = document.createElement("input");         

     file.setAttribute("type", "file")        

        file.setAttribute("name", "File")     

        file.setAttribute("runat", "server")    

        fileList.appendChild(file);

       var br = document.createElement("br");     

       fileList.appendChild(br);    

    }

3.后台代码

 HttpFileCollection files = HttpContext.Current.Request.Files;                         string returnvaluehttp://www.mamicode.com/= "";                         WebBindingList<ItemUploadFileModel> lst_file = new WebBindingList<ItemUploadFileModel>();                         string FilePath = CDHB.Common.Config.ConfigHelper.GetConfigString("FilePath");                         try                         {                             //取索引时从第三个开始取,                             for (int iFile = 0; iFile < files.Count; iFile++)                             {                                 HttpPostedFile uploadedFile = files[iFile];                                 if (!string.IsNullOrEmpty(uploadedFile.FileName))                                 {                                     string filename = uploadedFile.FileName;                                     string ext = filename.Substring(filename.LastIndexOf("."));                                     string filesize = "";                                     decimal n_filesize = 0;                                     if (uploadedFile.ContentLength > 0)                                     {                                         filesize = uploadedFile.ContentLength.ToString("n2") + "B";                                         if (uploadedFile.ContentLength > 1000)                                         {                                             n_filesize = Convert.ToDecimal(uploadedFile.ContentLength) / 1024;                                             filesize = n_filesize.ToString("n2") + "KB";                                         }                                         if (n_filesize > 1000)                                         {                                             n_filesize = n_filesize / 1024;                                             filesize = n_filesize.ToString("n2") + "M";                                         }                                         if (n_filesize > 1000)                                         {                                             n_filesize = n_filesize / 1024;                                             filesize = n_filesize.ToString("n2") + "G";                                         }                                         //获取当前年月份                                         string str = DateTime.Now.ToString("yyyyMM");                                         string strGui;                                         string strPath = HttpContext.Current.Server.MapPath(FilePath + str);                                         //如果源文件夹不存在则创建                                         if (!Directory.Exists(strPath))                                         {                                             Directory.CreateDirectory(strPath);                                         }                                         //重新赋文件名                                         string GuidName = Guid.NewGuid() + ext;                                         strGui = str + "/" + GuidName;                                         uploadedFile.SaveAs(Server.MapPath(FilePath + strGui));                                         returnvalue = http://www.mamicode.com/filename +"|" + FilePath + strGui;

                                        ItemUploadFileModel file = new ItemUploadFileModel(); ;                                         file.Id = Guid.NewGuid().ToString();                                         file.ItemId = rValue;                                         file.ItemFileName = filename;                                         file.ItemFileSize = filesize;                                         file.ItemFileSrc = http://www.mamicode.com/FilePath + strGui; file.UploadTime = DateTime.Now; file.CreateTime = DateTime.Now; lst_file.Add(file); }

                                }                             }                                                        ExhibitionRegister.AddItemFile(lst_file);                         }

 

注:

1.js添加input是,name属性必须

2.form添加 enctype="multipart/form-data"  。

 

Html上传多个文件