Exemple #1
0
        /// <summary>
        /// Handles the Click event of the btnUploadAttachment control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnUploadAttachment_Click(object sender, EventArgs e)
        {
            if (!_userCanAddAttachments)
            {
                // can't add attachments
                return;
            }

            if (!phAddNewAttachment.Visible)
            {
                // custom http put request, deny
                return;
            }

            if (_numberOfAttachments >= _forum.MaxNoOfAttachmentsPerMessage)
            {
                // # of attachments is already on maximum. Deny
                phUploadResult.Visible = true;
                lblError.Visible       = true;
                lblSuccess.Visible     = false;
                lblError.Text          = String.Format("You can't add another attachment to this message. Maximum # of attachments per message: {0}. Current # of attachments: {1}", _forum.MaxNoOfAttachmentsPerMessage, _numberOfAttachments);
                return;
            }

            byte[] fileContents = fuUploader.FileBytes;
            int    lengthInKB   = fileContents.Length / 1024;

            if (_forum.MaxAttachmentSize < (lengthInKB))
            {
                // attachment is too big
                phUploadResult.Visible = true;
                lblError.Visible       = true;
                lblSuccess.Visible     = false;
                lblError.Text          = String.Format("The attachment is too big. Maximum size: {0} KB. Attachment size: {1} KB", _forum.MaxAttachmentSize, lengthInKB);
                return;
            }

            if (fileContents.Length <= 0)
            {
                // file is empty
                phUploadResult.Visible = true;
                lblError.Visible       = true;
                lblSuccess.Visible     = false;
                lblError.Text          = "The attachment is empty, the size is 0 bytes.";
                return;
            }

            string fileName = Path.GetFileName(fuUploader.FileName);

            if (fileName.Length > 255)
            {
                // too big, chop off
                fileName = fileName.Substring(fileName.Length - 200);
            }

            MessageManager.AddAttachment(_message.MessageID, fileName, fileContents, SessionAdapter.CanPerformForumActionRight(_forum.ForumID, ActionRights.GetsAttachmentsApprovedAutomatically));
            phUploadResult.Visible = true;
            lblError.Visible       = false;
            lblSuccess.Visible     = true;
            lblSuccess.Text        = string.Format("Upload of attachment '{0}' with size {1} was successful.", fileName, fileContents.Length.ToString("N0"));

            BindAttachments();
        }