Recently I faced the need of programming a control for uploading multiple files attached to a SharePoint list item. Following this video example I could easily create an ASP.NET control to upload multiple files to a web server. Then I just addapted the code to upload the files attached to the SharePoint list item and the final result is as follows:
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();
}
I hope this helps!
0 comments:
Post a Comment