Go Back

Using the Telerik Radupload control

The other day, I needed to rewrite an upload function for the backend of a custom content management system photo gallery. Instead of using the standard upload control, I decided to use Telerik Radupload for easy setup for web design and look and feel.

The RadUpload control is real friendly to set up and can be use for single/multi file upload with integrated file validation. The integrated validation can check the file type by setting the AllowedFileExtensions property. The ControlObjectsVisibility determines what objects are shown in the control like the Add and Delete button. The Add creates a new file input box, and delete removes that box. The photo upload function only require one upload input box so I set the visiblity to none. The TargetFolder is the upload destination.

<telerik:RadUpload ID="RadUpload1" Runat="server"
ControlObjectsVisibility="None"
TargetFolder="~/gallery"
AllowedFileExtensions=".gif,.jpg,.jpeg">
</telerik:RadUpload>

Code Behind:

//Created a string that watches for new file names
string newfilename;

protected void cmdUpload_Click(object sender, EventArgs e)
{

RadUpload RadUpload1 = RadUpload)FormView1.FindControl("RadUpload1");
foreach (UploadedFile f in RadUpload1.UploadedFiles)
{
// Call to photo resize
// Call to thumbnail creation

string filename;
//if newfilename is not null, there is a new filename
if (newfilename != null)
{
filename = newfilename;
}
else
{
filename = f.GetName();
}

//set thumbnail on the front end formview

}
}

//The RapUpload controls will call RadUpload1_FileExists if there is another file with the same name

protected void RadUpload1_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
{
int counter = 1;

UploadedFile file = e.UploadedFile;

RadUpload RadUpload1 = (RadUpload)FormView1.FindControl("RadUpload1");

string targetFolder = Server.MapPath(RadUpload1.TargetFolder);
string targetFileName = System.IO.Path.Combine(targetFolder,
file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());

//loop until the an available filename is found, increase the number by 1 to the end of the file name each time

while (System.IO.File.Exists(targetFileName))
{
counter++;
targetFileName = System.IO.Path.Combine(targetFolder,
file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());

//Set newfilename value to the new file name
newfilename = file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension();
}

file.SaveAs(targetFileName);

//now the process is going back to cmdUpload_Click with a value loaded into newfilename
}

  • Facebook
  • Twitter
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Add diigo bookmark

Comments  2

  • Banshi 30 Dec

    Thanks for Such a usefull Post...
  • Berty 24 Jan

    Are you sure ?
Post a comment!
  1. Formatting options