How to upload file from Asp.Net to MOSS?
public bool UploadToMoss(HttpPostedFile filename)
{
bool blStatus = true;
try
{
SPSite siteCollection = null;
SPWeb topLevelSite = null;
string strDocLibraryLink = System.Configuration.ConfigurationManager.AppSettings["SPDocLibrary"].ToString(); //Server Name.........
siteCollection = new SPSite(strDocLibraryLink);
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings["TopLevelSite"].ToString()];
topLevelSite.AllowUnsafeUpdates = true;
SPFolder objFolder = topLevelSite.GetFolder(strDocLibraryName);
byte[] bufDoc = null;
string strFileName = Path.GetFileName(filename.FileName);
int nLen = filename.ContentLength;
bufDoc = new byte[nLen];
Stream oStream = filename.InputStream;
oStream.Read(bufDoc, 0, nLen);
System.GC.AddMemoryPressure(200024);
SPFile file = objFolder.Files.Add(strFileName, oStream, true);
file.Update();
}
catch (Exception ex)
{
blStatus = false;
throw;
}
finally
{
System.GC.RemoveMemoryPressure(200024);
}
return blStatus;
}
public bool UploadToMoss(HttpPostedFile filename)
{
bool blStatus = true;
try
{
SPSite siteCollection = null;
SPWeb topLevelSite = null;
string strDocLibraryLink = System.Configuration.ConfigurationManager.AppSettings["SPDocLibrary"].ToString(); //Server Name.........
siteCollection = new SPSite(strDocLibraryLink);
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings["TopLevelSite"].ToString()];
topLevelSite.AllowUnsafeUpdates = true;
SPFolder objFolder = topLevelSite.GetFolder(strDocLibraryName);
byte[] bufDoc = null;
string strFileName = Path.GetFileName(filename.FileName);
int nLen = filename.ContentLength;
bufDoc = new byte[nLen];
Stream oStream = filename.InputStream;
oStream.Read(bufDoc, 0, nLen);
System.GC.AddMemoryPressure(200024);
SPFile file = objFolder.Files.Add(strFileName, oStream, true);
file.Update();
}
catch (Exception ex)
{
blStatus = false;
throw;
}
finally
{
System.GC.RemoveMemoryPressure(200024);
}
return blStatus;
}