📄 adddownload.cs
字号:
namespace ASPNET.StarterKit.Communities.Downloads {
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using ASPNET.StarterKit.Communities;
//*********************************************************************
//
// AddDowload Class
//
// Represents the Add Download page. Enables users to list new files.
//
//*********************************************************************
public class AddDownload : ContentAddPage {
string _skinFileName = "Downloads_AddDownload.ascx";
string _sectionContent = "ASPNET.StarterKit.Communities.Downloads.DownloadsSection";
HtmlInputFile txtDownloadFile;
TextBox txtDownloadTitle;
TextBox txtDownloadBriefDescription;
HtmlTextBox txtDownloadFullDescription;
TopicPicker dropTopics;
//*********************************************************************
//
// SkinLoadDownload
//
// The skin load event happens after a page skin has been loaded.
// Here, we grab the necessary controls from the page skin.
//
//*********************************************************************
void SkinLoadDownload(Object s, SkinLoadEventArgs e) {
// Find the Form so that we can change the EncType of the post.
HtmlForm form = (HtmlForm)Page.FindControl("PageForm");
form.Enctype="multipart/form-data";
// Find Download Title
txtDownloadTitle = (TextBox)GetControl(e.Skin, "txtDownloadTitle" );
// Find Topics Panel and DropDownList
dropTopics = (TopicPicker)GetControl(e.Skin, "dropTopics");
//Find Brief Description
txtDownloadBriefDescription = (TextBox)GetControl(e.Skin, "txtDownloadBriefDescription" );
//Find Full Description
txtDownloadFullDescription= (HtmlTextBox)GetControl(e.Skin, "txtDownloadFullDescription" );
// Find the download upload button
txtDownloadFile = (HtmlInputFile)GetControl(e.Skin, "txtDownloadFile");
}
//*********************************************************************
//
// SubmitDownload Method
//
// This method is raised by clicking the Add button in the Add
// form. It adds a new file to the database.
//
//*********************************************************************
private void SubmitDownload(Object s, EventArgs e) {
if (Page.IsValid) {
// Check moderation status
ModerationStatus moderationStatus = ModerationStatus.Approved;
if (objSectionInfo.EnableModeration && !objUserInfo.MayModerate)
moderationStatus = ModerationStatus.Pending;
// Get Topic
int topicID = -1;
if (objSectionInfo.EnableTopics)
topicID = Int32.Parse(dropTopics.SelectedItem.Value);
// Get the file data
HttpPostedFile theFile = txtDownloadFile.PostedFile;
int fileSize = theFile.ContentLength;
byte[] fileBytes = new byte[fileSize];
System.IO.Stream myStream = theFile.InputStream;
myStream.Read(fileBytes,0,fileSize);
// Get file name
// The filename returns the entire client path.
// Filter for both Windows and Unix file paths.
Regex fileNameRE = new Regex("\\\\|/");
string[] fileNameSplit = fileNameRE.Split(theFile.FileName);
string _fileName = fileNameSplit[fileNameSplit.GetUpperBound(0)];
// Add the file
int contentPageID = DownloadsUtility.AddDownload
(
objUserInfo.Username,
objSectionInfo.ID,
moderationStatus,
txtDownloadTitle.Text,
txtDownloadBriefDescription.Text,
txtDownloadFullDescription.Text,
topicID,
_fileName,
fileBytes
);
// Show warning message if moderation enabled
if (objSectionInfo.EnableModeration && !objUserInfo.MayModerate)
Context.Response.Redirect(CommunityGlobals.CalculatePath("Messages_Message.aspx?message=moderation"));
// Otherwise, redirect to default page and send notifications
Context.Server.ScriptTimeout = 10 * 60;
Context.Response.Redirect(CommunityGlobals.CalculatePath("Default.aspx"), false);
Context.Response.Flush();
Context.Response.Close();
NotifyUtility.SendNotifications(objSectionInfo.ID, contentPageID, txtDownloadTitle.Text, objUserInfo.Username);
Context.Response.End();
}
}
//*********************************************************************
//
// AddDownload Constructor
//
// Calls the base SkinnedCommunityControl constructor
// and assigns the default page skin.
//
//*********************************************************************
public AddDownload() : base() {
// Assign a default skin file name
SkinFileName = _skinFileName;
// Specify Section Content
SectionContent = _sectionContent;
// Wire-up event handlers
this.SkinLoad += new SkinLoadEventHandler(SkinLoadDownload);
this.Submit += new SubmitEventHandler(SubmitDownload);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -