Upload file to Document Library
using (SPSite objSite = SPContext.Current.Site)
{
using (SPWeb objWeb = objSite.OpenWeb())
{
//adding file to document library
objWeb.AllowUnsafeUpdates = true;
//Get the folder the document library
SPFolder fldr = objWeb.GetFolder(“MKSDocLibrary”);
//Get the file collection inside the folder
SPFileCollection files = fldr.Files;
//TODO:Before adding file to doc library check the filename using pquery.
//Add new file in folder
SPFile sps = files.Add(“MKSDocLibrary” + “/” + fileName, fileContents, true);
//Set the DirectoryId of the owner of this file
sps.Item[“Title”] = strGiveTitleName;
if (sps.Item.Fields.ContainsField(“YourCustomFieldName”))
{
sps.Item[“YourCustomFieldName “] = “WriteSomeThing”;
}
sps.Item.Update();
}
}
This is to demonstrate how to upload file from InfoPath 2007
byte[] attachmentNodeBytes = Convert.FromBase64String(strBinaryData);
if (attachmentNodeBytes.Length <= 0)
{
return;
}
// Position 20 contains a DWORD indicating the length of the
// filename buffer. The filename is stored as Unicode so the
// length is multiplied by 2.
//Reference:
int fnLength = attachmentNodeBytes[20] * 2;
byte[] fnBytes = new byte[fnLength];
// The actual filename starts at position 24 . . .
for (int i = 0; i < fnBytes.Length; i++)
{
fnBytes[i] = attachmentNodeBytes[24 + i];
}
// Convert the filename bytes to a string. The string
// terminates with so the actual filename is the
// original filename minus the last character !
char[] charFileName = System.Text.UnicodeEncoding.Unicode.GetChars(fnBytes);
string fileName = new string(charFileName);
fileName = fileName.Substring(0, fileName.Length – 1);
// The file is located after the header, which is 24 bytes long
// plus the length of the filename.
byte[] fileContents = new byte[attachmentNodeBytes.Length – (24 + fnLength)];
for (int i = 0; i < fileContents.Length; ++i)
{
fileContents[i] = attachmentNodeBytes[24 + fnLength + i];
}