I am using a relative path and using iis. I ran the application with Firebug and in debug mode the path to the handler was correct (see correctpathtoraduploadhandlerindebug.gif). When I ran it out of debug, the path to the RadUploadHandler was incorrect (see correctpathtoraduploadhandleroutofdebug.gif). Please ignore the other two files uploaded; I made a mistake and wasn't able to delete them.
Below is some of my code which may help. Please help me with this it is getting urgent.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Net;
using
System.Windows;
using
System.Windows.Controls;
using
System.Windows.Documents;
using
System.Windows.Input;
using
System.Windows.Media;
using
System.Windows.Media.Animation;
using
System.Windows.Shapes;
using
System.IO;
using
System.ComponentModel;
using
System.Collections.ObjectModel;
using
System.Windows.Browser;
using
System.ServiceModel;
using
System.Windows.Threading;
using
System.Diagnostics;
using
System.ServiceModel.Channels;
namespace
_4Cast
{
public
partial
class
UploadFilesControl : UserControl
{
private
string
_UploadType =
string
.Empty;
private
string
_UploadServiceUrl =
"../RadSilverlightUploadHandler.ashx"
;
private
string
_FileFilter =
"All Files (*.*)|*.*"
;
private
int
_MaxFileSizeInKilobytes = 3072;
private
int
_MaxUploadSizeInKilobytes = 10240;
private
int
_ParentId = 0;
private
string
strSelectedFileName =
string
.Empty;
private
Brush _BackColour;
private
OpenFileDialog dialog =
new
OpenFileDialog();
private
string
_UploadDestination =
"UserUploads/"
;
private
string
_UploadButtonText =
"Upload"
;
private
static
Telerik.Windows.Controls.RadUpload ruAttachments =
new
Telerik.Windows.Controls.RadUpload();
private
double
UserControlHeight = 190;
private
double
UserControlWidth = 400;
private
List<
string
> FilesMarkedForDeletion =
new
List<
string
>();
public
UploadFilesControl()
{
InitializeComponent();
Loaded += UploadFilesControl_Loaded;
Unloaded += UploadFilesControl_Unloaded;
}
void
UploadFilesControl_Loaded(
object
sender, RoutedEventArgs e)
{
try
{
ruAttachments.UploadServiceUrl = UploadServiceUrl;
ruAttachments.MaxFileSize = MaxFileSizeInKilobytes * 1024;
ruAttachments.MaxUploadSize = MaxFileSizeInKilobytes * 1024;
ruAttachments.Filter = FileFilter;
ruAttachments.TargetFolder = UploadDestination;
ruAttachments.IsAutomaticUpload =
true
;
ruAttachments.OverwriteExistingFiles =
true
;
ruAttachments.IsPauseEnabled =
false
;
ruAttachments.UploadServiceUrl =
"../RadSilverlightUploadHandler.ashx"
;
ruAttachments.FileUploadStarting += ruAttachments_FileUploadStarting;
ruAttachments.UploadStarted += ruAttachments_UploadStarted;
ruAttachments.UploadFinished += ruAttachments_UploadFinished;
ruAttachments.ProgressChanged += ruAttachments_ProgressChanged;
ruAttachments.FileUploadFailed += ruAttachments_FileUploadFailed;
ruAttachments.FileCountExceeded += ruAttachments_FileCountExceeded;
ruAttachments.FileTooLarge += ruAttachments_FileTooLarge;
btnUpLoad.Content = UploadButtonText;
if
(!
double
.IsNaN(Height) || Height > 0)
{
UserControlHeight = Height;
}
if
(!
double
.IsNaN(Width) || Width > 0)
{
UserControlWidth = Width;
}
LayoutRoot.Height = UserControlHeight;
LayoutRoot.Width = UserControlWidth;
lstFiles.Height = LayoutRoot.Height - (brdHeader.Height + btnUpLoad.Height);
}
catch
{
}
}
void
UploadFilesControl_Unloaded(
object
sender, RoutedEventArgs e)
{
try
{
ruAttachments.FileUploadStarting -= ruAttachments_FileUploadStarting;
ruAttachments.UploadStarted -= ruAttachments_UploadStarted;
ruAttachments.UploadFinished -= ruAttachments_UploadFinished;
ruAttachments.ProgressChanged -= ruAttachments_ProgressChanged;
ruAttachments.FileUploadFailed -= ruAttachments_FileUploadFailed;
ruAttachments.FileCountExceeded -= ruAttachments_FileCountExceeded;
ruAttachments.FileTooLarge -= ruAttachments_FileTooLarge;
}
catch
{
}
}
public
bool
IsDirty
{
get
{
return
lstFiles.Items.Count > 0;
}
}
public
List<
string
> UploadedFiles
{
get
{
List<
string
> uf =
new
List<
string
>();
for
(
int
i = 0; i < lstFiles.Items.Count; i++)
{
uf.Add(lstFiles.Items[i].ToString());
}
return
uf;
}
}
void
ruAttachments_FileUploadStarting(
object
sender, Telerik.Windows.Controls.FileUploadStartingEventArgs e)
{
e.FileParameters.Add(
"UserId"
, App.WcfPushClient.UserId);
}
private
void
ruAttachments_UploadStarted(
object
sender, Telerik.Windows.Controls.UploadStartedEventArgs e)
{
lstFiles.Visibility = Visibility.Collapsed;
spProgress.Visibility = Visibility.Visible;
}
void
ruAttachments_ProgressChanged(
object
sender, RoutedEventArgs e)
{
try
{
pb.Value += ruAttachments.CurrentSession.CurrentFileProgress;
int
value = Math.Max(0, Math.Min(100, Convert.ToInt32((pb.Value / (pb.Maximum - pb.Minimum)) * 100)));
loadingPercentage.Text = value.ToString() +
" %"
;
if
(Convert.ToInt32((pb.Value / (pb.Maximum - pb.Minimum) * 100)) == 100)
{
loadingText.Text =
"Loaded "
;
}
}
catch
{
}
}
void
ruAttachments_FileUploadFailed(
object
sender, Telerik.Windows.Controls.FileUploadFailedEventArgs e)
{
MessageBox.Show(e.ErrorMessage,
"Upload Error"
, MessageBoxButton.OK);
}
void
ruAttachments_FileTooLarge(
object
sender, Telerik.Windows.Controls.FileEventArgs e)
{
MessageBox.Show(
"Unable to upload file ("
+ e.SelectedFile.Name +
") because at "
+ e.SelectedFile.Size.ToString() +
"bytes, it exceeds the maximum upload size of "
+ ruAttachments.MaxFileSize.ToString() +
"bytes."
,
"Upload Error"
, MessageBoxButton.OK);
}
void
ruAttachments_FileCountExceeded(
object
sender, RoutedEventArgs e)
{
MessageBox.Show(
"Unable to complete uploading all files because the maximum number of files that can be uploaded ("
+ ruAttachments.MaxFileCount +
") has been exceeded."
,
"Upload Error"
, MessageBoxButton.OK);
}
private
void
ruAttachments_UploadFinished(
object
sender, RoutedEventArgs e)
{
lstFiles.Visibility = Visibility.Visible;
spProgress.Visibility = Visibility.Collapsed;
pb.Value = pb.Minimum;
loadingText.Text =
"Loading... "
;
FillListBoxWithUploadFiles();
}
private
void
btnUpLoad_Click(
object
sender, RoutedEventArgs e)
{
ruAttachments.ShowFileDialog();
}
private
void
lstFiles_KeyDown(
object
sender, KeyEventArgs e)
{
try
{
switch
(e.Key)
{
case
Key.Delete:
if
(lstFiles.SelectedItems !=
null
&& lstFiles.SelectedItems.Count > 0)
{
for
(
int
i = 0; i < lstFiles.SelectedItems.Count; i++)
{
for
(
int
r = 0; r < ruAttachments.Items.Count; r++)
{
Telerik.Windows.Controls.RadUploadItem rui = ruAttachments.Items[r]
as
Telerik.Windows.Controls.RadUploadItem;
if
(rui !=
null
)
{
if
(rui.FileName == lstFiles.SelectedItems[i].ToString())
{
FilesMarkedForDeletion.Add(lstFiles.SelectedItems[i].ToString());
ruAttachments.Items.Remove(rui);
}
}
}
}
lstFiles.Items.Clear();
FillListBoxWithUploadFiles();
DeleteFilesMarkedForDeletion();
}
break
;
}
}
catch
{
}
}
public
void
ClearUploadedFiles()
{
UploadedFiles.Clear();
lstFiles.Items.Clear();
spProgress.Visibility = Visibility.Collapsed;
}
private
void
FillListBoxWithUploadFiles()
{
try
{
for
(
int
i = 0; i < ruAttachments.Items.Count; i++)
{
Telerik.Windows.Controls.RadUploadItem rui = ruAttachments.Items[i]
as
Telerik.Windows.Controls.RadUploadItem;
if
(rui !=
null
&& !lstFiles.Items.Contains(rui.FileName))
{
lstFiles.Items.Add(rui.FileName);
}
}
}
catch
{
}
}
public
Brush BackColour
{
get
{
return
_BackColour;
}
set
{
_BackColour = value;
LayoutRoot.Background = _BackColour;
}
}
public
string
UploadDestination
{
get
{
if
(UploadType.Length > 0 && !_UploadDestination.EndsWith(UploadType))
{
_UploadDestination += UploadType;
}
return
_UploadDestination;
}
}
public
string
UploadType
{
get
{
//Ensure contains only 1 trailing '/'
_UploadType = _UploadType.TrimEnd(System.IO.Path.AltDirectorySeparatorChar) + System.IO.Path.AltDirectorySeparatorChar;
return
_UploadType;
}
set
{
_UploadType = value;
}
}
public
string
UploadServiceUrl
{
get
{
return
_UploadServiceUrl;
}
set
{
_UploadServiceUrl = value;
}
}
public
string
UploadButtonText
{
get
{
return
_UploadButtonText;
}
set
{
_UploadButtonText = value;
}
}
public
string
FileFilter
{
get
{
return
_FileFilter;
}
set
{
_FileFilter = value;
dialog.Filter = _FileFilter;
}
}
public
int
ParentId
{
get
{
return
_ParentId;
}
set
{
_ParentId = value;
}
}
public
int
MaxFileSizeInKilobytes
{
get
{
return
_MaxFileSizeInKilobytes;
}
set
{
_MaxFileSizeInKilobytes = value;
}
}
public
int
MaxUploadSizeInKilobytes
{
get
{
return
_MaxUploadSizeInKilobytes;
}
set
{
_MaxUploadSizeInKilobytes = value;
}
}
public
void
DeleteFilesMarkedForDeletion()
{
try
{
FilesMarkedForDeletion.ForEach(f =>
{
//Don't delete current choice.
if
(f.Length > 0)
{
try
{
App.FileManagementClient.DeleteUploadedFileAsync(UploadDestination.Replace(System.IO.Path.AltDirectorySeparatorChar, System.IO.Path.DirectorySeparatorChar), f, App.WcfPushClient.UserId);
}
catch
(CommunicationException commEx)
{
FaultException faultEx = commEx
as
FaultException;
if
(faultEx ==
null
)
{
App.FileManagementClient.DeleteUploadedFileAsync(UploadDestination.Replace(System.IO.Path.AltDirectorySeparatorChar, System.IO.Path.DirectorySeparatorChar), f, App.WcfPushClient.UserId);
}
}
}
});
FilesMarkedForDeletion.Clear();
}
catch
{
}
}
}
}