Exemple #1
0
    protected void gvFileAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
        case "Attach":
        {
            if (gvFileAttachment.FooterRow != null)
            {
                FileUpload fu     = (FileUpload)gvFileAttachment.FooterRow.FindControl("fileUpload");
                Literal    litMsg = (Literal)gvFileAttachment.FooterRow.FindControl("addAttachmentMsg");
                if (fu.HasFile)
                {
                    FileInfo fInfo = new FileInfo(fu.PostedFile.FileName);

                    if (!FileUploadHelper.IsFileForbidden(fInfo.Extension))
                    {
                        int fileSize    = (fu.PostedFile.ContentLength + 512) / 1024;
                        int maxFileSize = int.Parse(ConfigurationManager.AppSettings["MaxFileSize"].ToString());

                        if (fileSize < maxFileSize)
                        {
                            string original = fu.FileName.ToString().Trim();
                            string actual   = FileUploadHelper.GetAlternativeFileName(fInfo.Extension);

                            try
                            {
                                fu.SaveAs((Constant.FILEATTACHMENTSFOLDERDIR) + actual);
                                Attach(original, actual);
                            }
                            catch
                            {
                                litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File cannot be uploaded.";
                            }
                        }
                        else
                        {
                            litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File size exceeds limit(" + maxFileSize + "KB).";
                        }
                    }
                    else
                    {
                        litMsg.Text = BR + "&nbsp;&nbsp;&nbsp;" + BULLET + " File cannot be uploaded. The file type is forbidden to be uploaded.";
                    }
                }
            }
        } break;

        case "Remove":
        {
            Remove(int.Parse(e.CommandArgument.ToString()));
        } break;

        case "Download":
        {
            string[] args = e.CommandArgument.ToString().Split(new char[] { '|' });
            string   path = Constant.FILEATTACHMENTSFOLDERDIR;
            FileHelper.DownloadFile(this.Page, path, args[0], args[1]);
        } break;
        }
    }