Thursday, January 20, 2011

Add file to List (or) File Upload to a Sharepoint list

Here is the code piece to handle file upload to a Sharepoint list.

SPSite site = SPContext.Current.Site;
using (SPWeb web = site.OpenWeb("MySite"))
{                
    SPFolder targetFoler = web.Lists["Shared Documents"].RootFolder;
    Stream fStream = fileUploader.PostedFile.InputStream;
    byte[] contents = new byte[fStream.Length];

    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close();

    targetFoler.Files.Add(fileUploader.FileName, contents);                
}

Hope this helps.

No comments:

Post a Comment