Programmatically Upload a Document in Document Library

Below is the code to upload a document in a document libarary.

public void DocumentUpload(SPWeb site)

if (FileUploadControl.HasFile)

SPFolder folder = site.GetFolder("Document_Library_Name");

SPFileCollection files = folder.Files;

Stream fStream = FileUpload1.PostedFile.InputStream; //path of the file to upload

byte[] contents = new byte[fStream.Length];

fstream.position =0;

fStream.Read(contents, 0, (int)fStream.Length);


string Filename = FileUpload1.FileName;

string URL = SPContext.Current.Site.Url + "/Document_Library_Name/" + Filename;

SPFile currentFile = files.Add(URL, contents);


  1. This comment has been removed by a blog administrator.



This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at .

Copyright (c) 2010 @ All rights are reserved.Do Not Copy.

@ Learning