SilverLight中的OpenFileDialog?

SilverLight中的OpenFileDialog?,silverlight,xaml,binding,Silverlight,Xaml,Binding,我已经从Silverlight教程网站下载了OpenFileDialog(文件上传功能)的代码。我正在使用ESRI API创建一个silverlight应用程序,我希望将文件上传功能合并到其中。我已将确切的代码复制到我的应用程序中,运行时没有错误,但由于某些原因,我的应用程序没有执行这行代码“c.OpenWriteAsync(Ub.Uri)” 编辑2:我在升级我下载的通用处理程序(receiver.ashx)时注意到另一件事,它的第一行有以下内容,而我的通用处理程序没有 我不知道为什么我的代码

我已经从Silverlight教程网站下载了OpenFileDialog(文件上传功能)的代码。我正在使用ESRI API创建一个silverlight应用程序,我希望将文件上传功能合并到其中。我已将确切的代码复制到我的应用程序中,运行时没有错误,但由于某些原因,我的应用程序没有执行这行代码“c.OpenWriteAsync(Ub.Uri)”

编辑2:我在升级我下载的通用处理程序(receiver.ashx)时注意到另一件事,它的第一行有以下内容,而我的通用处理程序没有

我不知道为什么我的代码没有触发它:(

这是我的密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Markup;
using System.Windows.Shapes;
using System.ComponentModel;
using ESRI.ArcGIS.Client;
using System.Windows.Controls.Primitives;
using System.IO;
using System.IO.IsolatedStorage;


namespace DataTool
{
  public partial class MainPage : UserControl
  {
  public MainPage()
  {
  InitializeComponent();
  Loaded += new RoutedEventHandler(MainPage_Loaded);
  }

  void MainPage_Loaded(object sender, RoutedEventArgs e)
  {
   // HtmlPage.RegisterScriptableObject("SilverlightLearn", this);
  }

  [ScriptableMember]
  private void btnService_Click(object sender, RoutedEventArgs e)
  {
  }

  private void btnUpload_Click(object sender, RoutedEventArgs e)
  {
  OpenFileDialog Dialog = new OpenFileDialog();
  Dialog.Multiselect = false;
  Dialog.Filter = "All Files|*.*";

  bool? SelFil = Dialog.ShowDialog();

  if (SelFil != null && SelFil == true)
  {
  string selectedfilename = Dialog.File.Name;
  UploadFile(selectedfilename, Dialog.File.OpenRead());

  }
  else
  {
  //do something else
  }
  }
  private void StoreIso(string fileName, Stream data)
  {

  }

  private void UploadFile(string fileName, System.IO.Stream data)
  {
   // WebClient Wbc = new WebClient();
  UriBuilder Ub = new UriBuilder("http://localhost:63461/DataTool/datareceiver.ashx");
  Ub.Query = string.Format("filename={0}", fileName);

  WebClient c = new WebClient();
  c.OpenWriteCompleted += (sender, e) =>
  {
  PushData(data, e.Result);
  e.Result.Close();
  data.Close();
  };
  c.OpenWriteAsync(Ub.Uri);
  }
  private void PushData(Stream input, Stream output)
  {

  byte[] buffer = new byte[4096];
  int bytesRead;

  while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)
  {
  output.Write(buffer, 0, bytesRead);
  }

  }

  }
}
这是我的datareceiver.ashx代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace DataTool.Web
{
  /// <summary>
  /// Summary description for datareceiver
  /// </summary>
  public class datareceiver : IHttpHandler
  {

  public void ProcessRequest(HttpContext context)
  {
  string filename = context.Request.QueryString["filename"].ToString();

  using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename)))
  {
  SaveFile(context.Request.InputStream, fs);
  }

  }
  public void SaveFile(Stream st, FileStream fs)
  {
  byte[] buffer = new byte[4096];
  int bytesRead;

  while ((bytesRead = st.Read(buffer, 0, buffer.Length)) != 0)
  {
  fs.Write(buffer, 0, bytesRead);
  }
  }

  public bool IsReusable
  {
  get
  {
  return false;
  }
  }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.IO;
名称空间DataTool.Web
{
/// 
///datareceiver的摘要说明
/// 
公共类数据接收器:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
字符串文件名=context.Request.QueryString[“filename”].ToString();
使用(FileStream fs=File.Create(context.Server.MapPath(“~/App\u Data/”+filename)))
{
保存文件(context.Request.InputStream,fs);
}
}
公共void保存文件(Stream st、FileStream fs)
{
字节[]缓冲区=新字节[4096];
int字节读取;
while((bytesRead=st.Read(buffer,0,buffer.Length))!=0)
{
fs.Write(缓冲区,0,字节读取);
}
}
公共布尔可重用
{
得到
{
返回false;
}
}
}
}
我已经一步一步地浏览了下载的示例代码和我的代码,发现我的代码没有执行OpenWriteAsync语句。下载的代码在.net 3.5或3.0 framework中,我将其升级到了4.0

编辑:
请在此处找到示例

这很简单,请检查以下代码

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt";
if (dlg.ShowDialog() == DialogResult.OK)
{
    using (StreamReader reader = dlg.SelectedFile.OpenText())

        // Store file content in 'text' variable
        string text = reader.ReadToEnd();
    }
}


C# Example 2: Copy files to the application's isolated storage.  
using System.Windows.Controls;
using System.IO;
using System.IO.IsolatedStorage;
...

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "All files (*.*)|*.*";
dlg.EnableMultipleSelection = true;
if (dlg.ShowDialog() == DialogResult.OK) {
    // Save all selected files into application's isolated storage
    IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
    foreach (FileDialogFileInfo file in dlg.SelectedFiles) {
        using (Stream fileStream = file.OpenRead()) {
            using (IsolatedStorageFileStream isoStream =
                new IsolatedStorageFileStream(file.Name, FileMode.Create, iso)) {

                // Read and write the data block by block until finish
                while(true) {
                    byte[] buffer = new byte[100001];
                    int count = fileStream.Read(buffer, 0, buffer.Length);
                    if (count > 0) {
                        isoStream.Write(buffer, 0, count);
                    }
                    else {
                        break;
                    }
                }
            }
         }
    }
}