Upload multiple files attached to a SharePoint list item.
SPWeb mySite = SPContext.Current.Web;SPList myList = mySite.Lists["myList"];SPListItem myListItem = myList.Items.Add();
myListItem["Title"] = "myTitle";...
if (HttpContext.Current.Request.Files.Count > 0){HttpFileCollection uploads = HttpContext.Current.Request.Files;SPAttachmentCollection attachments;
for (int i = 0; i < uploads.Count; i++){Stream fs = uploads[i].InputStream;byte[] fileContents = new byte[fs.Length];fs.Read(fileContents, 0, (int)fs.Length);fs.Close();attachments = myListItem.Attachments;string fileName = Path.GetFileName(uploads[i].FileName);attachments.Add(fileName, fileContents);}myListItem.Update();}
myListItem["Title"] = "myTitle";...
if (HttpContext.Current.Request.Files.Count > 0){HttpFileCollection uploads = HttpContext.Current.Request.Files;SPAttachmentCollection attachments;
for (int i = 0; i < uploads.Count; i++){Stream fs = uploads[i].InputStream;byte[] fileContents = new byte[fs.Length];fs.Read(fileContents, 0, (int)fs.Length);fs.Close();attachments = myListItem.Attachments;string fileName = Path.GetFileName(uploads[i].FileName);attachments.Add(fileName, fileContents);}myListItem.Update();}
No comments:
Post a Comment