Programmatically upload a Document to SharePoint Library
using (SPSite siteCollection = new SPSite("SiteURL"))
{
using (SPWeb spWeb = siteCollection.OpenWeb())
{
SPList spList = spWeb.Lists.TryGetList("Shared Documents");
string fileName = "new resume.doc";
FileStream fileStream = null;
Byte[] fileContent = null;
try
{
string docPath =@"D:\Kannan\"; //physical location of the file
fileStream = File.OpenRead(docPath + fileName);
fileContent = new byte[Convert.ToInt32(fileStream.Length)];
fileStream.Read(fileContent, 0, Convert.ToInt32(fileStream.Length));
spList.RootFolder.Files.Add(spList.RootFolder.Url + "/" + fileName, fileContent, true);
spList.Update();
}
catch (Exception ex)
{
}
finally
{
if (fileStream != null)
{
fileStream.Close();
}
}
}
}
Comments
Post a Comment