Uploading Word Document to a Document Library programmatically using Stream
public void UploadDocument(Stream stream)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite spSite = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
byte[] newfileBytes = new byte[stream.Length];
stream.Read(newfileBytes, 0, (int)stream.Length);
stream.Close();
spList = spSubWeb.Lists[Constants.MOMLibraryName];
SPDocumentLibrary spDocLib = (SPDocumentLibrary)spList;
SPFolder myLibrary = spSubWeb.GetFolder(spSubWeb.Url + "/" + spDocLib.Title);
fileName = "UploadedDocument.doc";
SPFile spFile = spSubWeb.GetFile(spSubWeb.Url + "/" + spList.Title + "/" + fileName);
spSubWeb.AllowUnsafeUpdates = true;
if (!spFile.Exists)
{
newFile = myLibrary.Files.Add(fileName, newfileBytes, true);
}
newFile.Item.Update();
spSubWeb.AllowUnsafeUpdates = false;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
No comments:
Post a Comment