If you need to upload files to a SharePoint document library programmatically the following code accomplishes that need.

SPSite site = new SPSite(URL);
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates=true;
string path =”"; 
string []fileName =filePath.PostedFile.FileName.Split(’\\’);
int length = fileName.Length;

string file=fileName[length-1];

SPFolder folder = web.GetFolder(LibraryName);
SPFileCollection files=folder.Files;

Stream fStream = filePath.PostedFile.InputStream;

byte[] MyData= new byte[fStream.Length];
fStream.Read(MyData, 0, (int)fStream.Length);
fStream.Close();
SPFile fff= files.Add(file,MyData);
web.AllowUnsafeUpdates=false;

2 Comments to “Code For Uploading File to Document Library”

  1. Louis says:

    Thank you, this is what I was looking for.

  2. [...] public links >> filename Code For Uploading File to Document Library First saved by adrianjordan | 1 days ago FTP a Mainframe dataset or a file using C#.NET First [...]