HTML uploads multiple files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.