본문 바로가기

Silverlight

[Silverlight3 간단예제] SaveFileDialog 다운로드를 하자!!


안녕하세요 인디 입니다. 저도 Silverlight3 를 공부 하면서 조금씩 공유하도록 하겠습니다. 원래 C# 을 몰랐었는데 이번에 실버라이트 하면서 배워서 많이 부족합니다.


기존 실버라이트2 에서는 OpenFileDialog 를 제공하고 있서 쉽게 업로드와 같은 기능을 구현할수  있었습니다. 하지만 그와는 반대로 다운로드 할수 있는 기능이 없어 js 등을 이용하여 편법으로 파일 등을 다운로드 하였습니다. 하지만 실버라이트 3에서 부터는 SavaFileDialog 를 제공하기 때문에 이제는 쉽게 파일을 다운로드 할수 있습니다.



------------------------------------------------------ [xaml]-----------------------------------------------------<UserControl x:Class="SilverlightApplication1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="Collapsed" x:Name="ProgBox" Orientation="Horizontal">
            <ProgressBar Width="200" Height="20" x:Name="Prog" Minimum="0" Maximum="100"/>
            <TextBlock x:Name="ProgCaption" Margin="10,0"/>
        </StackPanel>
        <Button x:Name="SaveButton" Content="SAVE" Width="100" Height="30" />
    </Grid>
</UserControl>



------------------------------------------------------[code]-----------------------------------------------------
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.IO;
namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        WebClient wc = new WebClient();
        SaveFileDialog sfd = new SaveFileDialog();
        public MainPage()
        {
            sfd.Filter = "JPEG File | *.jpg";
           
            InitializeComponent();
            this.SaveButton.Click += new RoutedEventHandler(SaveButtonClick);
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCompleted);
            wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
        }
       
        /// 다운로드 상태바
        void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            Prog.Value = e.ProgressPercentage;
            ProgCaption.Text=string.Format( "{0} %",e.ProgressPercentage);
        }
        /// 다운로드가 완료 되었을시 ( 파일저장)
        void OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (!e.Cancelled)
            {
                   
                using (Stream fs = sfd.OpenFile())
                {
                   
                    int length = Convert.ToInt32(e.Result.Length);
                    byte[] byteResult = new byte[length];
                    e.Result.Read(byteResult, 0, length);
                    fs.Write(byteResult, 0, byteResult.Length);
                    fs.Close();
                }
                MessageBox.Show("저장이 완료되었습니다.");
                ProgBox.Visibility = Visibility.Collapsed;
                SaveButton.Visibility = Visibility.Visible;
            }
        }
       
        /// 버튼 클릭시 다운로드를 요청 합니다.
        void SaveButtonClick(object sender, RoutedEventArgs e)
        {
            if (true == sfd.ShowDialog())
            {
                Uri url=new Uri("http://www.indreams.co.kr/silverlight.jpg");
               
                wc.OpenReadAsync(url);
                ProgBox.Visibility = Visibility.Visible; ;
                SaveButton.Visibility = Visibility.Collapsed;
               
            }
        }
       
    }
}


여러파일을 같이 다운받을수 있음 더 좋았을 텐데요. 아직까지는 아쉬운 점이 많네요 ^^ 그럼 좋은 하루 되세요.


[출처] 실버라이트 네이버카페