private void AddMoreColumns() { dtAttachFile = new DataTable("Files"); dcAttachFile = new DataColumn("FileName", Type.GetType("System.String")); dtAttachFile.Columns.Add(dcAttachFile); dcAttachFile = new DataColumn("FilePath", Type.GetType("System.String")); dtAttachFile.Columns.Add(dcAttachFile); } //----------------------------------------------------------- HttpFileCollection hfc = Request.Files; for (int i = 0; i < hfc.Count; i++) { HttpPostedFile hpf = hfc[i]; if (dtAttachFile == null) { AddMoreColumns(); } if (hpf.ContentLength > 0) { drAttachFile = dtAttachFile.NewRow(); string _tempFolder = Environment.GetEnvironmentVariable("TEMP"); string _fileTime = DateTime.Now.ToFileTime().ToString(); string _fileName = System.IO.Path.GetFileName(hpf.FileName); string _newfilePath = _fileTime + "~" + _fileName;
string _filepath = _tempFolder + "\\" + _newfilePath; hpf.SaveAs(_filepath); drAttachFile["FileName"] = _fileName; drAttachFile["FilePath"] = _filepath; dtAttachFile.Rows.Add(drAttachFile); } } 2. Upload the files from temp directory to SharePoint List int _dtAttachFilecount = dtAttachFile.Rows.Count; SPList myList = this._myTeamSite.Lists["L1"]; //L1 SharePoint List SPListItem newItem = myList.Items.Add(); foreach (DataRow drAttachFile in dtAttachFile.Rows) { fileName = drAttachFile["Filename"].ToString(); strFilepath = drAttachFile["FilePath"].ToString(); StreamReader sr = new StreamReader(strFilepath); Stream fStream = sr.BaseStream; contents = new byte[fStream.Length]; fStream.Read(contents, 0, (int)fStream.Length); fStream.Close(); newItem.Attachments.Add(fileName, contents); //Attach the files to sharepoint List System.IO.File.Delete(strFilepath); //3. Delete the files from temp directory. } this._myTeamSite.AllowUnsafeUpdates = true; newItem.Update(); //Commit the attachment this._myTeamSite.AllowUnsafeUpdates = false; } At first step we need to upload the files in temp directory, but for differentiate all files we can append the file name with timestamp and then upload it to temp directory. once we upload the files from temp to SPList we can remove it from temp directory,
References: http://www.c-sharpcorner.com/UploadFile/sarav82/MOSS11072007065009AM/MOSS.aspx Jayant Sharma