Programmatically upload file to SharePoint Document Library
Method 1 :
Stream fStream = upfile.PostedFile.InputStream;
byte[] contents = new
byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
string Filename = upfile.FileName;
string destUrl = SPContext.Current.Site.Url
+ "/" + myLibrary.Title + "/" + Filename;
oweb.Files.Add(destUrl, contents);
Method 2 : with metadata
SPDocumentLibrary myLibrary =
oweb.Lists[attachmentsLib] as SPDocumentLibrary;
byte[] fileBytes = upfile.FileBytes;
string strDestUrl = myLibrary.RootFolder.Url + "/" + upfile.FileName;
SPFile
destFile = myLibrary.RootFolder.Files.Add(strDestUrl, fileBytes, true);
destFile.Item["DocID"] = "1";
destFile.Update();
destFile.Item.Update();
destFile.Update();
oweb.AllowUnsafeUpdates
= false;
Comments
Post a Comment