Silverlight ネットワーク操作

 SilverLight自身だけでファイルのアップロードができません、ここではWCFの力を借りて、ファイルのアップロード方法を紹介します。

サービス側

 ファイルアップロードするためのデータコンタクトクラスを定義します。  

[DataContract]
public class UploadFile
{
   [DataMember]
   public string FileName { get; set; }

   [DataMember]
   public byte[] File { get; set; }
}

サービス提供する側は下記のように、サービスを定義します。

IService1.svcファイル:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace FileUploadService  
{
   [ServiceContract]
   public interface IService1  
   { 
       [OperationContract]
       void SaveFile(UploadFile UploadFile);
   }
}

Service1.svc.csファイル:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Configuration;
using System.Web;
using System.ServiceModel.Activation;
using System.IO; 
namespace FileUploadService
{
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] 
  public class Service1 : IService1
  { 
      public void SaveFile(UploadFile UploadFile)
      {
          FileStream FileStream = new FileStream("E:\\FileUpload\\" + UploadFile.FileName, FileMode.Create);
          FileStream.Write(UploadFile.File, 0, UploadFile.File.Length); 
          FileStream.Close();
          FileStream.Dispose();
      } 
  }
}

Silverlight側

 Silverlight側下記のようにあるボタンクリックイベント内で「OpenFileDialog」利用して、ファイルデータを取得できます。

private void button1_Click(object sender, RoutedEventArgs e)
{
   OpenFileDialog fileDialog = new OpenFileDialog();
   fileDialog.Multiselect = false;
   fileDialog.Filter = "All Files|*.*";
   bool? retval = fileDialog.ShowDialog();
   if (retval != null && retval == true)
   {
       Stream strm = fileDialog.File.OpenRead();
       byte[] Buffer = new byte[strm.Length];
       strm.Read(Buffer, 0, (int)strm.Length);
       strm.Dispose();
       strm.Close();
       
       UploadFile file = new UploadFile();
       file.FileName = fileDialog.File.Name;
       file.File = Buffer;
       Service1Client proxy = new Service1Client();
       proxy.SaveFileAsync(file);  
       proxy.SaveFileCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_SaveFileCompleted);  
   }
}

void proxy_SaveFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)  
{  
  if (e.Error == null)
  {
      txtFileDisplayName.Text = fileDialog.File.Name + "アップロードしました。";   
  }   
}  

Web.Config中WCFの設定は「SilverLight上WCFサービスの設定」参照ください。

トラブルシューティング

以上の実装で、実行する時、下記のようなエラーメッセージが発生する可能性があります。メッセージに説明している通りに、「readerQuotas」の「MaxArrayLength」の値を設定すれば解決できます。

 メッセージ パラメーター http://locahost/Server1:uploadFile をシリアル化解除しようとしているときにエラーが発生しました。InnerException メッセージは 'オブジェクト 型 UploadFile の のシリアル化を解除しているときにエラーが発生しました。XML データの読み取り中に最大配列長クォータ (16384) を超えました。このクォータを増やすには、XML リーダーの作成時に使用される XmlDictionaryReaderQuotas オブジェクトの MaxArrayLength プロパティを変更してください。' でした。詳細については InnerException を参照してください。 のシリアル化を解除しようとしているときに、フォーマッタから例外がスローされました

設定サンプル:

<bindings>
 <customBinding>
   <binding name="Services.customBinding1">
     <binaryMessageEncoding maxReadPoolSize="2147483647" maxSessionSize="2147483647" maxWritePoolSize="2147483647">
       <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
     </binaryMessageEncoding>
     <httpTransport transferMode="Streamed" keepAliveEnabled="false" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
   </binding>
 </customBinding>
</bindings>


中古車買取実績No.1ガリバー

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結解除 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:36:12 (1627d)

G|Cg|C@Amazon Yahoo yV

z[y[W yVoC[UNLIMITȂ1~] COiq COsیI