in aspx:
<asp:UpdatePanel ID="upfuAttachment" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="fuAttachment" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
in Code behind:
SPWeb oWeb = SPContext.Current.Web;
try
{
oWeb.AllowUnsafeUpdates = true;
if (fuAttachment.HasFile)
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
SPAttachmentCollection attachments;
for (int i = 0; i < uploads.Count; i++)
{
Stream fs = uploads[i].InputStream;
byte[] fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, (int)fs.Length);
fs.Close();
attachments = itemToAdd.Attachments;
string fileName = Path.GetFileName(uploads[i].FileName);
attachments.Add(fileName, fileContents);
}
}
oWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
//throw ex;
}
<asp:UpdatePanel ID="upfuAttachment" runat="server" UpdateMode="conditional">
<ContentTemplate>
<asp:FileUpload ID="fuAttachment" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
</asp:UpdatePanel>
in Code behind:
SPWeb oWeb = SPContext.Current.Web;
try
{
oWeb.AllowUnsafeUpdates = true;
if (fuAttachment.HasFile)
{
HttpFileCollection uploads = HttpContext.Current.Request.Files;
SPAttachmentCollection attachments;
for (int i = 0; i < uploads.Count; i++)
{
Stream fs = uploads[i].InputStream;
byte[] fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, (int)fs.Length);
fs.Close();
attachments = itemToAdd.Attachments;
string fileName = Path.GetFileName(uploads[i].FileName);
attachments.Add(fileName, fileContents);
}
}
oWeb.AllowUnsafeUpdates = false;
}
catch (Exception ex)
{
//throw ex;
}