Optimized Object model API code for uploading large files to a SharePoint document library
The following code block contains an optimized code for uploading large files (>500 MB) to a SharePoint document library. This code may be used to circumvent Out of Memory exceptions when the process loads the entire file in memory for uploading.
//Get the FileName from the fully qualified path
string strFileName = Path.GetFileName(strFileFullName);
// siteURL = URL of the web on which the file will be uploaded
using (SPSite sitecollection = new SPSite(siteURL))
{
using (SPWeb site = new SPSite(siteURL).OpenWeb())
{
site.AllowUnsafeUpdates = true;
StreamReader reader = new StreamReader(strFileFullName);
//docLibGuid = GUID of the Document Library where the file will be uploaded
SPDocumentLibrary targetDocLib = (SPDocumentLibrary)site.Lists[docLibGuid];
string libraryRelativePath = targetDocLib.RootFolder.ServerRelativeUrl;
string libraryPath = sitecollection.MakeFullUrl(libraryRelativePath);
string documentPath = libraryPath + "/" + strFileName;
strFolderUrl = libraryPath + "/";
site.Files.Add(documentPath, reader.BaseStream, true);
site.AllowUnsafeUpdates = false;
reader.Close();
reader.Dispose();
}
}
Comments
- Anonymous
June 05, 2008
I think there are a few tweaks to make this code even better, but it seems pretty good. One issue is