码迷,mamicode.com
首页 > Web开发 > 详细

Html上传多个文件

时间:2015-01-02 17:24:08      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

1.页面

<span style="float: left;text-align:left;" id="spanFileList">                                 <input type="button" value="添加附件" 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 returnvalue = "";                         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 = filename + "|" + FilePath + strGui;

                                        ItemUploadFileModel file = new ItemUploadFileModel(); ;                                         file.Id = Guid.NewGuid().ToString();                                         file.ItemId = rValue;                                         file.ItemFileName = filename;                                         file.ItemFileSize = filesize;                                         file.ItemFileSrc = 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上传多个文件

标签:

原文地址:http://www.cnblogs.com/nygsb2014/p/4198546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!