前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >telerik upload 在silv

telerik upload 在silv

作者头像
py3study
发布2020-01-14 15:58:42
4310
发布2020-01-14 15:58:42
举报
文章被收录于专栏:python3

打开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;     } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档