To attach the file from Local Machine to SharePoint ListItem as an attachment, Import the System.IO namespace to use the Stream Class to read the file contents.
Use System.Web.UI.WebControls.FileUpload control is named as fileUpload in code.
Sample code for uploading the file from Local Machine to ListItem attachments as follows,
//Check the FileUpload control has the file
if (fileUpload.PostedFile != null)
{item = list.Items.Add();
item["Title"] = fileUpload.FileName;
//Read the Contents from the file in Local machine
Stream fs = fileUpload.PostedFile.InputStream;
byte[] fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, (int)fs.Length);
fs.Close();
// Add the file to the ListItem as an Attachment
SPAttachmentCollection attachments = item.Attachments;
string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);
attachments.Add(fileName, fileContents);
item.Update();
}

Is it possible to check out a file from sharepoint and modify it and check in back (Like in VSS). I have a requirement to modify a excel file in sharepoint and repost it again! is it possible! if yes kindly tell me how?
By: Arunan on June 30, 2008
at 2:44 pm
Check below office links for Check-out and check-in a file without coding in sharepoint.
http://office.microsoft.com/en-us/sharepointtechnology/HA101535701033.aspx?pid=CH101787551033
http://office.microsoft.com/en-us/sharepointtechnology/HA101535891033.aspx?pid=CH101787551033
By: ktskumar on July 1, 2008
at 11:18 am
Thanks for ur reply santhakumar!
i want to do it thru coding! I actually knew to do it without coding!
can u suggest a way for that
By: Arunan on July 3, 2008
at 4:26 pm
No need to add reference to system.io
Single line of code
item.Attachments.Add(fileUpload.FileName, fileUpload.FileBytes);
Hope this helps.
By: Shiva on August 30, 2008
at 2:20 am
That is correct. No need for system.io
I’ve triedd it and it works
By: Rav Tan on November 4, 2009
at 3:09 pm
Would it be possible to restrict the some of users not to open the attachment while viewing the list item.
By: sam on August 9, 2009
at 10:10 pm