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);

fStream.Close();

string Filename = FileUpload1.FileName;

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

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

1 comments:

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

    ReplyDelete

Disclaimer

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 sharepointprogrammingblogger@gmail.com .

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

@ Learning SharePoint.com