1. Page
<span style= "float:left;text-align:left;" id= "spanfilelist" > <input type= "button "Value=" Add Attachment "onclick=" AddFiles () "style=" margin-top:5px; margin-left:5px "/><br/> </span>
2.js
Add an attachment
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. Background code
httpfilecollection files = httpcontext.current.request.files; String returnvalue = ""; webbindinglist<itemuploadfilemodel> lst_file = new Webbindinglist<itemuploadfilemodel> (); string FilePath = CDHB. Common.Config.ConfigHelper.GetConfigString ("FilePath"); try {&NBSP;&NBSP;&NBSP;&NBsp; //Fetch, from the third start when indexing 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 >) { N_ FileSize = Convert.todecimal (uploadedfile.contentlength)/1024; filesize = n_filesize. ToString ("n2") + "KB"; } if (n_filesize > +) { n_filesize = n_filesize/1024; FileSize = n_filesize. ToString ("n2") + "M"; } if (N_filesize > &nbs)p; { n_filesize = n_filesize/1024; filesize = n_filesize. ToString ("n2") + "G"; } // Get current year month string str = DateTime.Now.ToString ("yyyymm"); string Strgui; String strpath = HttpContext.Current.Server.MapPath (FilePath + str); //Create If the source folder does not exist if (! Directory.Exists (strpath)) { Directory.CreateDirectory (strpath); } //re-assign file name 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;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; file. Createtime = datetime.now; Lst_file. ADD (file); }
}} exhibitionregister.additemfile (Lst_ file); }
Note:
1.js Add input Yes, the Name property must be
2.form add enctype= "Multipart/form-data".
HTML uploads multiple files