2018/05/08

SampleCMS 附件管理共用元件說明:附件管理共用元件 AttachFileManagerLogic 使用方式

上一篇 附件管理功能概述 提到了 SampleCMS 附件管理的四項主要功能「儲存(更新)附件資訊與檔案、檢視附件資訊、下載附件檔案、刪除附件檔案」。

這一篇繼續說明附件管理共用元件 AttachFileManagerLogic 在上述功能中「儲存(更新)附件資訊與檔案、檢視附件資訊、刪除附件檔案」負責的工作。

下圖為「網站架構管理」之中網頁資料的附件設定頁 Article-Attach.aspx 畫面,

使用者透過附件設定頁操作「儲存(更新)附件資訊與檔案、檢視附件資訊」這兩項功能,
而附件設定頁網頁程式利用附件管理共用元件 AttachFileManagerLogic 來完成這兩項功能。

下列以附件設定頁的程式碼 Article-Attach.aspx.cs 說明 AttachFileManagerLogic 使用方式。

初始化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public partial class Article_Attach : System.Web.UI.Page
{
    // ...略...
    protected AttachFileManagerLogic attFileMgr;
 
    protected void Page_PreInit(object sender, EventArgs e)
    {
        // ...略...
        attFileMgr = new AttachFileManagerLogic(this.Context);
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!attFileMgr.Initialize(c.qsAttId, c.qsArtId))
        {
            string errMsg = ResUtility.GetErrMsgOfAttFileErrState(attFileMgr.GetErrState());
            Master.ShowErrorMsg(errMsg);
            return;
        }
 
        // ...略...
    }

Line 9: 產生 AttachFileManagerLogic 物件,為了讓元件使用 context.Server ,因此需帶入 this.Context 。

Line 14: 使用 Initialize(c.qsAttId, c.qsArtId) 初始化附件管理共用元件,帶入的兩個參數為網址參數 attid 和 artid 的值。attid 為附件代碼,編輯現有附件時使用。artid 為網頁代碼,新增附件時使用,新增附件時網址參數只會有 artid 而沒有 attid ,當網址參數沒給 attid 時,c.qsAttId 會輸出 Guid.Empty ,也就是 00000000-0000-0000-0000-000000000000 ,而附件管理共用元件以這個值來判斷是新增模式,否則就是編輯模式。

在新增附件時,這個共用元件將自動產生新的排序編號值。

在編輯附件時,這個共用元件將從資料庫取回指定附件代碼的附件資料,並且暫存在物件屬性中(參考下圖 AttachFileManagerLogic 類別圖),提供給網頁程式使用。

Line 16: 當附件管理共用元件初始化失敗時,使用 GetErrState() 可取回錯誤訊息用的列舉值,再透過 ResUtility.GetErrMsgOfAttFileErrState() 取回列舉值對應的錯誤訊息字串(依照後台系統的語系回應)。

呈現檔案資訊、檔案類型限制名單:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
private void LoadExtAndMimeLimitations()
{
    if (attFileMgr.FileExtLimitations != null && attFileMgr.FileExtLimitations.Count > 0)
    {
        string extCombined = string.Join(", ", attFileMgr.FileExtLimitations.ToArray());
        ltrExtLimitations.Text = extCombined;
        ExtLimitationsArea.Visible = true;
 
        if (attFileMgr.FileMimeLimitations != null && attFileMgr.FileMimeLimitations.Count > 0)
        {
            string acceptList = string.Join(",", attFileMgr.FileMimeLimitations.ToArray());
            fuPickedFile.Attributes.Add("accept", acceptList);
        }
    }
}
 
private void DisplayAttachFileData()
{
    if (c.qsAct == ConfigFormAction.edit)
    {
        txtSortNo.Text = attFileMgr.SortNo.ToString();
        chkDontDelete.Checked = attFileMgr.DontDelete;
        ltrPostAccount.Text = attFileMgr.PostAccount;
        ltrPostDate.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}", attFileMgr.PostDate);
        // ...略...
        ltrFileSavedName.Text = attFileMgr.FileSavedName;
        // ...略...
 
        if (attFileMgr.MdfDate.HasValue)
        {
            ltrMdfAccount.Text = attFileMgr.MdfAccount;
            ltrMdfDate.Text = string.Format("{0:yyyy-MM-dd HH:mm:ss}", attFileMgr.MdfDate);
        }
 
        //zh-TW
        if (LangManager.IsEnableEditLangZHTW())
        {
            txtAttSubjectZhTw.Text = attFileMgr.AttSubjectZhTw;
            chkIsShowInLangZhTw.Checked = attFileMgr.IsShowInLangZhTw;
        }
 
        //en
        if (LangManager.IsEnableEditLangEN())
        {
            txtAttSubjectEn.Text = attFileMgr.AttSubjectEn;
            chkIsShowInLangEn.Checked = attFileMgr.IsShowInLangEn;
        }
 
        btnSave.Visible = true;
    }
    else if (c.qsAct == ConfigFormAction.add)
    {
        txtSortNo.Text = attFileMgr.SortNo.ToString();
        btnSave.Visible = true;
    }
}

Line 1~15: 將 FileExtLimitations 字串清單的內容呈現在畫面上的檔案類型限制區給使用者看。

另外 FileMimeLimitations 字串清單是用在 <input type="file"> 上傳檔案控制項的 accept 屬性中,用來自動篩選檔案類型。

Line 17~56: 將附件管理共用元件暫存在屬性中的檔案資訊呈現在畫面上。

儲存附件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
protected void btnSave_Click(object sender, EventArgs e)
{
    // ...略...
    try
    {
        // ...略...
        attFileMgr.SortNo = Convert.ToInt32(txtSortNo.Text);
        attFileMgr.AttSubjectZhTw = txtAttSubjectZhTw.Text;
        attFileMgr.AttSubjectEn = txtAttSubjectEn.Text;
        attFileMgr.IsShowInLangZhTw = chkIsShowInLangZhTw.Checked;
        attFileMgr.IsShowInLangEn = chkIsShowInLangEn.Checked;
        attFileMgr.DontDelete = chkDontDelete.Checked;
 
        result = attFileMgr.SaveData(fuPickedFile, c.GetEmpAccount());
 
        if (result)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", StringUtility.GetNoticeOpenerJs("Attach"), true);
        }
        else
        {
            string errMsg = ResUtility.GetErrMsgOfAttFileErrState(attFileMgr.GetErrState());
 
            if (errMsg == "")
            {
                errMsg = Resources.Lang.ErrMsg_SaveFailed;
            }
 
            Master.ShowErrorMsg(errMsg);
        }
        // ...略...
    }
    catch (Exception ex)
    {
        c.LoggerOfUI.Error("", ex);
        Master.ShowErrorMsg(ex.Message);
    }
}

Line 7~14: 將使用者填寫的值更新至附件管理共用元件的屬性,再透過呼叫 SaveData(FileUpload fu, string mdfAccount) 儲存附件資訊以及實體檔案。

Line 22: 當附件管理共用元件儲存失敗時,使用 GetErrState() 可取回錯誤訊息用的列舉值,再透過 ResUtility.GetErrMsgOfAttFileErrState() 取回列舉值對應的錯誤訊息字串(依照後台系統的語系回應)。

* 完整程式碼請參考 Article-Attach.aspx.cs


下圖為「網站架構管理」之中網頁資料的附件清單畫面,

下列以網頁內容頁的程式碼 Article-Node.aspx.cs 說明當使用者按下刪除附件時, AttachFileManagerLogic 的使用方式。

刪除附件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
protected void rptAttachFiles_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    // ...略...
    switch (e.CommandName)
    {
        case "Del":
            // ...略...
            AttachFileManagerLogic attFileMgr = new AttachFileManagerLogic(this.Context);
            result = attFileMgr.Initialize(attId, c.qsArtId);
 
            if (result)
            {
                result = attFileMgr.DeleteData();
                // ...略...
            }
 
            if (!result)
            {
                string errMsg = ResUtility.GetErrMsgOfAttFileErrState(attFileMgr.GetErrState());
 
                if (errMsg == "")
                {
                    errMsg = Resources.Lang.ErrMsg_DeleteAttachmentFailed;
                }
 
                Master.ShowErrorMsg(errMsg);
            }
 
            break;
        // ...略...
    }
    // ...略...
}

Line 8, 9: 初始化附件管理共用元件,attId 為刪除鈕所屬的附件資料的附件代碼。

Line 13: 初始化成功後,透過 DeleteData() 讓附件管理共用元件刪除附件資訊以及實體檔案。

Line 19: 當附件管理共用元件刪除附件失敗時,使用 GetErrState() 可取回錯誤訊息用的列舉值,再透過 ResUtility.GetErrMsgOfAttFileErrState() 取回列舉值對應的錯誤訊息字串(依照後台系統的語系回應)。

* 完整程式碼請參考 Article-Node.aspx.cs


照片管理共用元件 ArticlePictureManagerLogic 繼承 AttachFileManagerLogic ,延用附件管理的所有方法,只需改寫檔案限制類型名單、儲存的目錄位置、更換存取資料層的方法,在照片設定頁 Article-Picture.aspx 之中,其使用方式和 AttachFileManagerLogic 一模一樣。

附件管理元件關聯圖
在上列附件管理元件關聯圖中,ArticlePictureManagerLogic 列出的方法皆為有改寫的部分。

* 完整程式碼請參考 Article-Picture.aspx.cs




沒有留言:

張貼留言