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);
}}
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);
}}
This comment has been removed by a blog administrator.
ReplyDelete