Try
{
String filetoupload = " C: \ test.txt " ;
String shortpointsite = " Http: // localhost: 80/ " ;
String documentlibraryname = " Test " ;
Spsecurity. runwithelevatedprivileges ( Delegate ()
{
Using (Spsite osite = New Spsite (sharepointsite ))
{
Using (Spweb oweb = osite. rootweb)
{
Oweb. allowunsafeupdates = True ;
If (! System. Io. file. exists (filetoupload ))
Throw New Exception ( " File not found. " );
Spdocumentlibrary mylibrary = oweb. Lists [documentlibraryname]AsSpdocumentlibrary;
// Prepare to upload
Boolean replaceexistingfiles = True ;
String filename = system. Io. Path. getfilename (filetoupload );
Filestream = file. openread (filetoupload );
// upload document
spfile = mylibrary. rootfolder. files. add (oweb. URL. tostring () + " / " + mylibrary. title. tostring () + " / " + filenamewithoutext + fileextname, filestream, replaceexistingfiles);
// Commit
Spfile. Update ();
Mylibrary. Update ();
Oweb. Update ();
Oweb. allowunsafeupdates = False ;
}
}
});
}< br> catch (exception sperror)
{< br> response. write (sperror. message);
}