Wednesday, 6 November 2013

programmatically upload file to document library in sharepoint 2010

Share it Please

Code:-

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
  {

using (SPWeb web = site.OpenWeb())
  {

SPSecurity.RunWithElevatedPrivileges(delegate()
               {

SPDocumentLibrary documentLib = web.Lists["Shared Documents"] as SPDocumentLibrary;

Stream fStream = FileUpload1.PostedFile.InputStream;
byte[]  _byteArray = new byte[fStream.Length];
fStream.Read(_byteArray, 0, (int)fStream.Length);
fStream.Close();

web.AllowUnsafeUpdates = true;

string _fileUrl = documentLib.RootFolder.Url + "/" + FileUpload1.PostedFile.FileName;

bool IsOverwriteFile = true;
SPFile file = documentLib.RootFolder.Files.Add(_fileUrl, _byteArray, IsOverwriteFile);

SPListItem item = file.Item;
item["Title"] = FileUpload1.PostedFile.FileName;
item.Update();
file.Update();

web.AllowUnsafeUpdates = false;
           
      });        
 }
   
 }

No comments:

Post a Comment

Followers

Follow The Author