📄 attachmentbussiness.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 关于附件的业务层操作
/// </summary>
public class AttachmentBussiness
{
BBSTableTableAdapters.ArticleTableAdapter artTable;
string PhysicalPath;
public AttachmentBussiness()
{
artTable = new BBSTableTableAdapters.ArticleTableAdapter();
}
/// <summary>
/// 上传附件,并返回添加的ID
/// </summary>
/// <param name="att">附件实体</param>
/// <returns>添加是否成功</returns>
public bool AddAttachment(ref AttachmentEntity att,FileUpload fileUpload)
{
//上传文件
string attPath=att.PhysicalPath + @"\Attachment";
if (Common.CreateDirectory(attPath))
{
attPath += @"\" + DateTime.Now.Year.ToString();
att.AttachmentURL =@"/" +DateTime.Now.Year.ToString();
if (Common.CreateDirectory(attPath))
{
attPath += @"\" + DateTime.Now.Month.ToString();
att.AttachmentURL += @"/"+DateTime.Now.Month.ToString();
if (Common.CreateDirectory(attPath))
{
attPath += @"\" + DateTime.Now.Day.ToString();
att.AttachmentURL += @"/"+DateTime.Now.Day.ToString();
if (Common.CreateDirectory(attPath))//若创建目录成功
{
//上传文件
string fileName = Common.GenerateFileName();
string fileType=System.IO.Path.GetExtension(fileUpload.PostedFile.FileName);
att.AttachmentURL += @"/"+fileName+fileType;
attPath += @"\"+fileName + fileType;
fileUpload.SaveAs(attPath);
}
else//若创建不成功
return false;
}
else//若创建不成功
return false;
}
else//若创建不成功
return false;
}
else//若创建不成功
return false;
object attID = artTable.AddAttachment(att.AttachmentURL);
if (attID != null)
att.AttachmentID = Convert.ToInt32(attID);
return true;
}
/// <summary>
/// 根据ID删除附件纪录和附件文件
/// </summary>
/// <param name="att">附件实体</param>
public bool DeleteAttachmentByID(AttachmentEntity att)
{
//若该附件信息存在
if (GetAttachmentByID(ref att))
{
string fileName = att.PhysicalPath + @"\attachment"+att.AttachmentURL.Replace(@"/", @"\");
if (Common.DeleteFile(fileName))
return true;
else
return false;
}
else//否则
{
return false;
}
}
/// <summary>
/// 根据ID获取附件URL
/// </summary>
/// <param name="att">附件实体</param>
public bool GetAttachmentByID(ref AttachmentEntity att)
{
DataTable attURL = artTable.GetAttachmentByID(att.AttachmentID);
//若没有符合条件的纪录
if (attURL == null)
return false;
else//否则
{
att.AttachmentURL = attURL.Rows[0][0].ToString();
return true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -