ほんじゃらねっと

ダイエット中プログラマのブログ

Silverlightでファイルをアップロードして保存する

複数ファイルアップロードが結構簡単にできるみたい。
下記のサイトを参考(ほぼそのまま)に作ってみた。


Silverlight 4でドロップされたデータをサーバに保存
http://d.hatena.ne.jp/okazuki/20100330/1269949623


Silverlightでユーザがアップロードしたファイルをダウンロードする
http://d.hatena.ne.jp/coma2n/20080421/1208784501


ファイルを開くダイアログで選択された複数のファイルをサーバにアップロードして保存するサンプル。
WCF RIA Servicesのドメインサービスを使ってサーバ側と連携してる。


MainPage.xaml

<UserControl x:Class="SilverFileUpload.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
<StackPanel x:Name="LayoutRoot" Background="White">
<Button x:Name="OpenFileButton" Content="open file" Click="OpenFileButton_Click" />
<TextBox x:Name="MessageTextBox" Width="200" Margin="10" AcceptsReturn="True" Height="200" />
</StackPanel>
</UserControl>


MainPage.xaml.cs

using System;
using System.Text;
using System.IO;
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 SilverFileUpload.Web;
namespace SilverFileUpload
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void OpenFileButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Text Files (.txt)|*.txt|All Files (.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = true;
bool? isOKClicked = openFileDialog.ShowDialog();
if (isOKClicked == true)
{
IEnumerable<FileInfo> files = openFileDialog.Files;
StringBuilder sb = new StringBuilder();
foreach (FileInfo file in files)
{
using (var stream = file.OpenRead())
{
var ms = new MemoryStream();
byte[] buffer = new byte[1024 * 100];
while (stream.Read(buffer, 0, buffer.Length) != 0)
{
ms.Write(buffer, 0, buffer.Length);
}
var ctx = new FileUploadDomainContext();
ctx.Upload(file.Name, ms.ToArray(), result =>
{
// 終了後処理
}, null);
}
sb.Append(file.Name + "\r\n");
}
MessageTextBox.Text = sb.ToString();
}
}
}
}


FileUploadDomainService.cs

namespace SilverFileUpload.Web
{
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// TODO: アプリケーション ロジックを含むメソッドを作成します。
[EnableClientAccess()]
public class FileUploadDomainService : DomainService
{
public bool Upload(string fileName, byte[] data)
{
try
{
using (var w = new FileStream(Path.Combine(@"ファイル保存先ディレクトリ", fileName), FileMode.OpenOrCreate, FileAccess.Write))
{
w.Write(data, 0, data.Length);
}
return true;
}
catch
{
return false;
}
}
}
}


AppEngineでもサンプルを作ってみたいところ。