Exemple #1
0
    private void SaveAttachment()
    {
        if (this.FileUpload1.HasFile == false)
        {
            PageCommon.WriteJsEnd(this, "Please select a valid file.", PageCommon.Js_RefreshSelf);
        }

        string sMsg   = string.Empty;
        bool   bValid = PageCommon.ValidateUpload(this, this.FileUpload1, 1024 * 1024 * 20, out sMsg, ".gif", ".jpg", ".jpeg", ".pdf", ".png", ".doc", ".docx", ".xls", ".xlsx", ".zip");

        if (bValid == false)
        {
            PageCommon.WriteJsEnd(this, sMsg, PageCommon.Js_RefreshSelf);
        }

        string sSelEmailTemplateID = this.ddlEmailTemplate.SelectedValue;
        string sAttachName         = this.txtAttchName.Text.Trim();
        string sFileType           = this.ddlFileType.SelectedValue;

        LPWeb.Model.Template_Email_Attachments AttachModel = new LPWeb.Model.Template_Email_Attachments();
        AttachModel.TemplEmailId = Convert.ToInt32(sSelEmailTemplateID);
        AttachModel.Enabled      = true;
        AttachModel.Name         = sAttachName;
        AttachModel.FileType     = sFileType;
        AttachModel.FileImage    = this.FileUpload1.FileBytes;

        Template_Email_Attachments Template_Email_AttachmentsMgr = new Template_Email_Attachments();

        Template_Email_AttachmentsMgr.Add(AttachModel);
    }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List <LPWeb.Model.Template_Email_Attachments> DataTableToList(DataTable dt)
        {
            List <LPWeb.Model.Template_Email_Attachments> modelList = new List <LPWeb.Model.Template_Email_Attachments>();
            int rowsCount = dt.Rows.Count;

            if (rowsCount > 0)
            {
                LPWeb.Model.Template_Email_Attachments model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new LPWeb.Model.Template_Email_Attachments();
                    if (dt.Rows[n]["TemplAttachId"].ToString() != "")
                    {
                        model.TemplAttachId = int.Parse(dt.Rows[n]["TemplAttachId"].ToString());
                    }
                    if (dt.Rows[n]["TemplEmailId"].ToString() != "")
                    {
                        model.TemplEmailId = int.Parse(dt.Rows[n]["TemplEmailId"].ToString());
                    }
                    if (dt.Rows[n]["Enabled"].ToString() != "")
                    {
                        if ((dt.Rows[n]["Enabled"].ToString() == "1") || (dt.Rows[n]["Enabled"].ToString().ToLower() == "true"))
                        {
                            model.Enabled = true;
                        }
                        else
                        {
                            model.Enabled = false;
                        }
                    }
                    model.Name     = dt.Rows[n]["Name"].ToString();
                    model.FileType = dt.Rows[n]["FileType"].ToString();
                    if (dt.Rows[n]["FileImage"].ToString() != "")
                    {
                        model.FileImage = (byte[])dt.Rows[n]["FileImage"];
                    }
                    modelList.Add(model);
                }
            }
            return(modelList);
        }
 /// <summary>
 /// 更新一条数据
 /// </summary>
 public void Update(LPWeb.Model.Template_Email_Attachments model)
 {
     dal.Update(model);
 }
 /// <summary>
 /// 增加一条数据
 /// </summary>
 public int Add(LPWeb.Model.Template_Email_Attachments model)
 {
     return(dal.Add(model));
 }
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 校验页面参数

        bool bValid = PageCommon.ValidateQueryString(this, "TemplAttachId", QueryStringType.ID);
        if (bValid == false)
        {
            this.Response.End();
        }
        int TemplAttachId = Convert.ToInt32(this.Request.QueryString["TemplAttachId"]);

        #endregion

        #region 加载Email Template信息

        Template_Email_Attachments             Template_Email_AttachmentsMgr = new Template_Email_Attachments();
        LPWeb.Model.Template_Email_Attachments AttchModel = Template_Email_AttachmentsMgr.GetModel(TemplAttachId);
        if (AttchModel == null)
        {
            PageCommon.WriteJsEnd(this, "Invalid email attachment id.", "window.close();");
        }



        #endregion

        // 文件扩展名
        string Ext = "";
        if (AttchModel.FileType == "Word")
        {
            Ext = ".docx";
        }
        else if (AttchModel.FileType == "XLS")
        {
            Ext = ".xlsx";
        }
        else if (AttchModel.FileType == "JPGE")
        {
            Ext = ".jpg";
        }
        else
        {
            Ext = "." + AttchModel.FileType.ToLower();
        }

        // 文件名
        string sFileName = AttchModel.Name + Ext;

        this.Response.Clear();
        this.Response.ClearHeaders();
        this.Response.Buffer = false;
        if (AttchModel.FileType == "PDF")
        {
            this.Response.ContentType = "application/pdf";
        }
        else if (AttchModel.FileType == "Word")
        {
            this.Response.ContentType = "application/msword";
        }
        else if (AttchModel.FileType == "XLS")
        {
            this.Response.ContentType = "application/vnd.ms-excel";
        }
        else if (AttchModel.FileType == "ZIP")
        {
            this.Response.ContentType = "application/zip";
        }
        else
        {
            this.Response.ContentType = "application/octet-stream";
        }

        if (AttchModel.FileType == "ZIP" || AttchModel.FileType == "Word" || AttchModel.FileType == "XLS")
        {
            this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + sFileName);
        }
        else
        {
            this.Response.AppendHeader("Content-Disposition", "inline;filename=" + sFileName);
        }
        this.Response.BinaryWrite(AttchModel.FileImage);
        this.Response.Flush();

        //if (AttchModel.FileType == "Word" || AttchModel.FileType == "XLS" || AttchModel.FileType == "ZIP")
        //{
        //    PageCommon.WriteJsEnd(this, "", PageCommon.Js_CloseWindow);
        //}
    }