【问题标题】:OpenFileDialog in SilverLight?SilverLight 中的 OpenFileDialog?
【发布时间】:2011-08-15 05:23:10
【问题描述】:

我已经从 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;
  }
  }
  }
}

我已经浏览了下载的示例代码和我的代码STEP BY STEP,发现我的代码没有执行OpenWriteAsync 语句。下载的代码在 .net 3.5 或 3.0 框架中,我将其升级到 4.0。

编辑: 请在此处找到示例https://rapidshare.com/files/459667631/Testing.zip

【问题讨论】:

    标签: silverlight xaml binding


    【解决方案1】:

    这很简单,检查以下代码

    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;
                        }
                    }
                }
             }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 2010-12-16
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多