public partial class FileUpload : UserControl
{
protected MediaUploadUtility Uploader {
get { return _uploader; }
}
public FileUpload() {
InitializeComponent();
_uploader = new MediaUploadUtility();
_uploader.UploadProgressChanged += new UploadProgressChangedHandler(Uploader_UploadProgressChanged);
_uploader.UploadCompleted += new UploadCompletedHandler(Uploader_UploadCompleted);
_uploader.UploadCancelled += new UploadCancelledHandler(Uploader_UploadCancelled);
_uploader.IsApproximatingProgress = true;
_uploader.ProgressReportingMultiplier = 0.8;
_uploader.MaxUploadCount = BrowserUtil.Name == BrowserName.Firefox ? 6 : 2;
}
private void UploadFiles(IEnumerable<FileInfo> files) {
// Start upload
Progress.Value = 0;
ProgressText.Text = "0.00%";
Uploader.PerformUpload(files, new Uri(Configuration.UploadServiceAddress + "/Media/Upload", UriKind.Absolute),
"{BaseUri}/{FileType}/{FileKey}/{ChunkNumber}/{TotalChunks}?Name={FileName}");
// Add files to preview window
FileProgressContainer.Children.Clear();
foreach (MediaFileWrapper file in Uploader.MediaFiles) {
FileProgressContainer.Children.Add(new FileProgressDisplay(file));
}
}
private void CancelUpload() {
if (Uploader.IsWorking) {
UploadButton.Content = "Cancelling...";
UploadButton.IsEnabled = false;
Uploader.Cancel(new Uri(Configuration.UploadServiceAddress + "/Media/CancelUpload", UriKind.Absolute),
"{BaseUri}/{FileKey}");
}
}
private static string CreateFileFilter() {
// Get image extensions
string imageExtensions = string.Empty;
foreach (string ext in MediaFileWrapper.IMAGE_TYPES.Split(';')) {
if (imageExtensions.Length > 0) {
imageExtensions += ";";
}
imageExtensions += "*." + ext;
}
// Get video extensions
string videoExtensions = string.Empty;
foreach (string ext in MediaFileWrapper.VIDEO_TYPES.Split(';')) {
if (videoExtensions.Length > 0) {
videoExtensions += ";";
}
videoExtensions += "*." + ext;
}
// Create filter string
return string.Format("ImageFiles|{0}|Movie Files|{1}|All Supported Media|{0};{1}",
imageExtensions, videoExtensions);
}
private void UploadButton_Click(object sender, RoutedEventArgs e) {
switch (UploadButton.Content.ToString()) {
case "Upload!":
OpenFileDialog uploadDialog = new OpenFileDialog();
uploadDialog.Filter = CreateFileFilter();
uploadDialog.FilterIndex = 3;
uploadDialog.Multiselect = true;
if (uploadDialog.ShowDialog() == true) {
UploadButton.Content = "Cancel";
UploadAnimation.Begin();
UploadFiles(uploadDialog.Files);
}
break;
case "Cancel":
CancelUpload();
break;
}
}
private void Uploader_UploadCancelled(object sender, EventArgs e) {
Dispatcher.BeginInvoke(delegate() {
ProgressText.Text = "Cancelled";
UploadButton.Content = "Upload!";
UploadButton.IsEnabled = true;
UploadCompleteAnimation.Begin();
});
}
private void Uploader_UploadCompleted(object sender, EventArgs e) {
Dispatcher.BeginInvoke(delegate() {
ProgressText.Text = "Completed";
UploadButton.Content = "Upload!";
UploadButton.IsEnabled = true;
UploadCompleteAnimation.Begin();
});
}
private void Uploader_UploadProgressChanged(object sender, UploadProgressEventArgs e) {
Dispatcher.BeginInvoke(delegate() {
Progress.Value = e.TotalPercentComplete;
ProgressText.Text = string.Format("{0:n2}%", e.TotalPercentComplete);
foreach (FileProgress fileProgress in e.FileProgress) {
foreach (FileProgressDisplay fileDisplay in FileProgressContainer.Children) {
if (fileProgress.File.File == fileDisplay.UploadedFile.File) {
fileDisplay.Progress = fileProgress.Progress;
fileDisplay.ProgressText = string.Format("{0:n0}%", fileProgress.Progress);
break;
}
}
}
});
}
private readonly MediaUploadUtility _uploader;
}