Html上传多个文件
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" 。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。