telerik upload 在silverlight 3中的使用

打开SL工程添加引用Telerik.Windows.Controls.dll and Telerik.Windows.Controls.Input.dll.
以及在Page.xaml中添加RadUpload控件

<telerikInput:RadUpload  
  x:Name="radUpload"  
  Filter="All Files(*.*)|*.*"  
  FilterIndex="3"  
  IsAutomaticUpload="False"  
  OverwriteExistingFiles="True"  
  UploadServiceUrl="../RadUploadHandler.ashx"  
  TargetFolder="MyStorageFolder"  
  FileUploaded="radUpload_FileUploaded"  
  >  
</telerikInput:RadUpload>  
 

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 Telerik.Windows.Controls;
using Telerik.Windows;
using System.Windows.Media.Imaging;

namespace TelerikDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void radUpload_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            RadUploadSelectedFile uploadedFile = e.SelectedFile;
            // CustomData is a Dictionary property that stores the new file name in value of a key.
            // This key is set in the RadUploadHandler.ashx.cs
            string newFileName = e.HandlerData.CustomData["NewFileName"].ToString();

            if (this.radUpload.CurrentSession.FileNameUploadItem.ContainsKey(uploadedFile.Name))
            {
                RadUploadItem item = this.radUpload.CurrentSession.FileNameUploadItem[uploadedFile.Name];
                if (item != null)
                {
                    // Retrieve the TextBlock that will hold new file name
                    FrameworkElement element = GetCustomTagControl(item, "NewFileName");
                    if (element != null)
                    {
                        TextBlock textBlock = element as TextBlock;
                        if (textBlock != null)
                        {
                            textBlock.Text = newFileName;
                            textBlock.Visibility = Visibility.Visible;
                        }
                    }
                }
            }
        }

        private static FrameworkElement GetCustomTagControl(DependencyObject control, string name)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(control); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(control, i);
                FrameworkElement element = child as FrameworkElement;

                if (element != null)
                {
                    if (0 == string.Compare(element.Name, name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return element;
                    }
                }
                element = GetCustomTagControl(child, name);
                if (element != null)
                {
                    return element;
                }
            }
            return null;
        }
    }
}
打开RadUploadDemo.Web并且添加引用Telerik.Windows.RadUploadHandler.dll. 
在这之后,右击RadUploadDemo.Web选择添加新item,添加一个名为RadUploadHandler.ashx的 Generic Handler,也添加一个新文件夹名为MyStorageFolder

打开RadUploadHandler.ashx文件,你的handler需继承Telerik.Windows.RadUploadHandler并且重写一部分重要的函数

<%@ WebHandler Language="C#" Class="RadUploadHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

public class RadUploadHandler : Telerik.Windows.RadUploadHandler {

    private string newFileName = string.Empty;

    //public override void ProcessStream()
    //{
    //    if (this.IsNewFileRequest())
    //    {
    //        this.ResultChunkTag = string.Format("[{0;yyyymmdd.hhmmss}]",DateTime.Now);
    //    }
    //    else if (this.FormChunkTag != null)
    //    {
    //        this.ResultChunkTag = this.FormChunkTag;
    //    }
    //    base.ProcessStream();
    //}

    public override Dictionary<string, object> GetAssociatedData()
    {
        Dictionary<string, object> dict = base.GetAssociatedData();
        if (!string.IsNullOrEmpty(newFileName))
        {
            dict.Add("NewFileName", this.newFileName);
        }
        return dict;
    }

    public override string GetFilePath(string fileName)
    {
        fileName = base.GetFilePath(this.GetFileName(fileName));
        return fileName;
    }

    private string GetFileName(string fileName)
    {
        if (this.IsNewFileRequest())
        {
            this.ResultChunkTag = string.Format(" [{0:yyyymmdd_hhmmss}]", DateTime.Now);
        }

        else if (this.FormChunkTag != null)
        {
            this.ResultChunkTag = this.FormChunkTag;
        }

        if (this.ResultChunkTag != null)
        {
            int i = fileName.LastIndexOf('.');
            if (i >= 0)
            {
                fileName = fileName.Insert(i, this.ResultChunkTag);
            }
        }
        return this.newFileName = fileName;
    }
}

你可能感兴趣的:(upload,职场,silverlight,Telerik,休闲)