Attachments Removing Code
public void DeleteRemovedAttachments(SPAttachmentCollection attachments, CheckBoxList chk)
{
try
{
if (chk.Items.Count <= attachments.Count)
{
string strchkitem; string strattacheditem;
for (int k = attachments.Count - 1; k >= 0; k--)
{
bool filenotexist = false;
string attachedfile = attachments[k];
FileInfo objfileinfo = new FileInfo(attachedfile);
for (int l = 0; l < chk.Items.Count; l++)
{
string chklnkitem = chk.Items[l].Text;
if (chklnkitem.StartsWith("<a href='http://"))
chklnkitem = chk.Items[l].Text.Split('>')[1].Split('<')[0];
if (attachedfile == chklnkitem)
{
filenotexist = true;
strchkitem = chk.Items[l].Text;
break;
}
}
if (!filenotexist)
{
strattacheditem = attachments[k];
attachments.Delete(objfileinfo.Name);
}
}
}
}
catch (Exception ex)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language=\"javascript\">alert('" + ex.Message + "')</script>");
}
}
protected void btnAttachFile_Click(object sender, EventArgs e)
{
AttachFile(fuAttachments, chkAttachedFiles, btnRemoveFile);
}
protected void btnRemoveFile_Click(object sender, EventArgs e)
{
RemoveSelectedFiles(chkAttachedFiles, btnRemoveFile);
}
protected void btnKaizenSheetRemove_Click(object sender, EventArgs e)
{
RemoveSelectedFiles(chkKaizenSheet, btnKaizenSheetRemove);
}
private void AttachFile(FileUpload fuAttachmentControl, CheckBoxList chk, Button btnR)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (fuAttachmentControl.HasFile)
{
if (fuAttachmentControl.PostedFile != null && fuAttachmentControl.HasFile)
{
string fileName = Path.GetFileName(fuAttachmentControl.PostedFile.FileName);
bool exitornot = CheckToDuplicate(fileName);//Modified by prabhakar
if (exitornot)//Modified by prabhakar
Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language=\"javascript\">alert('The Attachment " + fileName + " already exists.Please select different file')</script>");
else
{
string tempFilePath = "";
if (!fileName.Contains("@"))
{
tempFilePath = System.IO.Path.GetTempPath().ToString() + "\\" + fuAttachmentControl.FileName;
fuAttachmentControl.SaveAs(tempFilePath);
if (hfFiles.Value == "")
{
hfFiles.Value = tempFilePath;
hfFileNames.Value = fuAttachmentControl.FileName;
}
else
{
hfFiles.Value = hfFiles.Value + "@" + tempFilePath;
hfFileNames.Value = hfFileNames.Value + "@" + fuAttachmentControl.FileName;
}
string[] attachedfiles = hfFiles.Value.Split('@');
string[] attachedfileNames = hfFileNames.Value.Split('@');
DataTable dtFiles = new DataTable();
dtFiles.Columns.Add("FileName");
dtFiles.Columns.Add("FilePath");
for (int i = 0; i < attachedfiles.Length; i++)
{
dtFiles.Rows.Add(attachedfileNames[i], attachedfiles[i]);
}
chk.Visible = true;
chk.DataSource = null;
chk.DataSource = dtFiles;
chk.DataValueField = "FilePath";
chk.DataTextField = "FileName";
chk.DataBind();
if (dtFiles.Rows.Count > 0)
btnR.Visible = true;
else
btnR.Visible = false;
}
}
}
}
});
}
catch (Exception ex)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language=\"javascript\">alert('" + ex.Message + "')</script>");
}
}
Comments