Telerik RadUpload示例

FileDetail.xaml
<controls:ChildWindow x:Class="SGSGHome.Views.File.FileDetail"

           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 

           xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"

           xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"

           Width="600" Height="400" 

           Title="文件上传">

    <Grid x:Name="LayoutRoot" Margin="2">

        <Grid.RowDefinitions>

            <RowDefinition Height="*"/>

            <RowDefinition Height="50"/>

        </Grid.RowDefinitions>

        <telerik:RadUpload 

            x:Name="FileUpload"

            IsMultiselect="False"

            MaxWidth="600"

            telerik:StyleManager.Theme="Summer" 

            Filter="文件(office;pdf;proe;)|*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.pdf;*.dwg;*.prt;"

            IsAutomaticUpload="false"

            OverwriteExistingFiles="True"

            TargetFolder="Upload"

            UploadServiceUrl="。。/。。/RadUploadHandler.ashx"

            FileUploaded="FileUpload_FileUploaded"

            FileUploadStarting="FileUpload_FileUploadStarting"

            AllowDrop="True"

         />

        <Button x:Name="CancelButton" Content="取消" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,0,0" Grid.Row="1" />

        <Button x:Name="OKButton" Content="确定" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,12,79,0" Grid.Row="1" />

    </Grid>

</controls:ChildWindow>
FileDetail.xaml.cs
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;



namespace SGSGHome.Views.File

{

    public partial class FileDetail : ChildWindow

    {

        List<string> extList = new List<string>() { 

            ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf",".dwg",".prt"

        };



        CoreService.CoreServiceClient client;



        //编辑状态

        public FormTag FormTag { get; set; }



        //分类ID

        public long CatID { get; set; }



        //重命名文件Guid

        public string FileGuid { get; set; }



        //物料编码

        public string MaterialCode { get; set; }



        //文件编号

        public string FileNo { get; set; }



        //版本

        public string FileVersion { get; set; }



        //是否加密文件

        public bool IsEncrypt { get; set; }



        //编辑记录实例

        public CoreService.FileInfo FileInfo { get; set; }

        public FileDetail()

        {

            InitializeComponent();

            this.Loaded += new RoutedEventHandler(FileDetail_Loaded);

        }



        void FileDetail_Loaded(object sender, RoutedEventArgs e)

        {

            client = new CoreService.CoreServiceClient();

            client.AddFileInfoCompleted += new EventHandler<CoreService.AddFileInfoCompletedEventArgs>(client_AddFileInfoCompleted);

            client.UpdateFileInfoCompleted += new EventHandler<CoreService.UpdateFileInfoCompletedEventArgs>(client_UpdateFileInfoCompleted);

        }



        void client_UpdateFileInfoCompleted(object sender, CoreService.UpdateFileInfoCompletedEventArgs e)

        {



        }



        void client_AddFileInfoCompleted(object sender, CoreService.AddFileInfoCompletedEventArgs e)

        {



        }



        private void OKButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult = true;

        }



        private void CancelButton_Click(object sender, RoutedEventArgs e)

        {

            this.DialogResult =  false;

        }



        private void FileUpload_FileUploaded(object sender, Telerik.Windows.Controls.FileUploadedEventArgs e)

        {

            if (FormTag == FormTag.Add)

            {

                var entity = new CoreService.FileInfo();

                entity.CategoryID = this.CatID;

                entity.MaterialCode = this.MaterialCode;

                entity.FileVersion = this.FileVersion;

                entity.FileNo = this.FileNo;

                entity.FileName = System.IO.Path.GetFileNameWithoutExtension(e.SelectedFile.Name);

                entity.FileExtend = e.SelectedFile.File.Extension;

                entity.FileGuid = this.FileGuid;

                entity.FileUrl = string.Format("http://{0}/FileDownloadHandler.ashx?Guid={1}", AppContext.Host, this.FileGuid);

                entity.CreatedBy = AppContext.LogonUserName;

                entity.CreatedDate = DateTime.Now;

                entity.IsEncrypt = this.IsEncrypt;

                client.AddFileInfoAsync(entity);

            }

            else

            {

                FileInfo.FileName = System.IO.Path.GetFileNameWithoutExtension(e.SelectedFile.Name);

                FileInfo.FileExtend = e.SelectedFile.File.Extension;

                FileInfo.FileGuid = this.FileGuid;

                FileInfo.IsEncrypt = this.IsEncrypt;

                FileInfo.FileUrl = string.Format("http://{0}/FileDownloadHandler.ashx?Guid={1}", AppContext.Host, this.FileGuid);

                client.UpdateFileInfoAsync(FileInfo);

            }

        }



        private void FileUpload_FileUploadStarting(object sender, Telerik.Windows.Controls.FileUploadStartingEventArgs e)

        {

            if (!extList.Any(x => x == e.SelectedFile.File.Extension))

            {

                MessageBox.Show("文件类型不正确");

                FileUpload.CancelUpload();

            }



            this.FileGuid = Guid.NewGuid().ToString();

            e.UploadData.FileName = this.FileGuid + e.SelectedFile.File.Extension;

        }

    }

}
RadUploadHandler.ashx.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;



namespace SGSGHome.Web

{

    /// <summary>

    /// RadUploadHandler 的摘要说明

    /// </summary>

    public class RadUploadHandler : Telerik.Windows.RadUploadHandler

    {

        public void ProcessRequest(HttpContext context) { }

        public bool IsReusable { get { return false; } }

    }

}
FileDownloadHandler.ashx.cs
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.IO;



namespace SGSGHome.Web

{

    /// <summary>

    /// FileDownloadHandler 的摘要说明

    /// </summary>

    public class FileDownloadHandler : IHttpHandler

    {



        public void ProcessRequest(HttpContext context)

        {

            var fileGuid = context.Request["Guid"];

            var dt = DBHelper.ExecuteDataTable(

                string.Format(@"

                                select top 1 FileFullName=FileName+FileExtend,FileGuidName=FileGuid+FileExtend 

                                from FileInfo where FileGuid='{0}'", fileGuid));



            var fileName = dt.Rows[0]["FileFullName"].ToString();

            var filePath = context.Server.MapPath("Upload/" + dt.Rows[0]["FileGuidName"].ToString());



            context.Response.ContentType = "application/octet-stream";

            context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName));

            context.Response.TransmitFile(filePath);

        }



        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}

 Reference:Telerik.Windows.RadUploadHandler.dll

你可能感兴趣的:(upload)