2012-06-05 20 views
0

我不知道爲什麼,但我的處理程序沒有被觸發。 (順便說一下,我對Silverlight仍然很陌生。)我按照教程here。我不確定我做錯了什麼...謝謝。 [使用Silverlight 3.0,VS2008]文件上傳不觸發處理程序?

MainPage.xaml中

<UserControl x:Class="MultiSelectFileUploader.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:DesignWidth="640" d:DesignHeight="480"> 
<Grid x:Name="LayoutRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <TextBox x:Name="tbUL" Grid.Row="0" Width="150" Margin="0 0 250 0"></TextBox> 
    <Button x:Name="btnUL" Grid.Row="0" Width="100" Click="btnUL_Click"></Button> 

    <ListBox Grid.Row="1" Width="200" Height="25" Margin="0 0 200 0"></ListBox> 
    <Button Grid.Row="1" Width="50" Margin="50 0 0 0"></Button> 

    <TextBox Grid.Row="2" Width="125" Margin="0 0 275 0"></TextBox> 
    <TextBox Grid.Row="2" Width="125" Margin="0 0 25 0"></TextBox> 
</Grid> 
</UserControl> 

MainPage.xaml.cs中

using System; 
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 System.IO; 

namespace MultiSelectFileUploader 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      btnUL.Content = "Upload"; 
     } 

     protected void btnUL_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false }; 

      if ((bool)ofd.ShowDialog()) // If a file is selected 
      { 
       UploadFile(ofd.File.Name, ofd.File.OpenRead()); 
      } 
     } 

     private void UploadFile(string FileName, Stream Data) 
     { 
      UriBuilder ub = new UriBuilder("http://localhost:64168/Receiver.ashx"); 
      ub.Query = string.Format("filename={0}", FileName); 

      WebClient wc = new WebClient(); 
      wc.OpenReadCompleted += (sender, e) => 
      { 
       PushData(Data, e.Result); 
       e.Result.Close(); 
       Data.Close(); 
      }; 
      wc.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); 
     } 
    } 
} 

Receiver.ashx

<%@ WebHandler Language="C#" CodeBehind="Receiver.ashx.cs" Class="MultiSelectFileUploader.Web.Receiver" %> 

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


public class Receiver : IHttpHandler 
{ 

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

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

    private void SaveFile(Stream Stream, FileStream FS) 
    { 
     byte[] buffer = new byte[4096]; 
     int bytesRead; 
     while ((bytesRead = Stream.Read(buffer, 0, buffer.Length)) != 0) 
      FS.Write(buffer, 0, bytesRead); 
    } 

    public bool IsReusable 
    { 
     get { return false; } 
    } 
} 

回答

1

我啞...

該行... wc.OpenReadCompleted + =(發件人,E)=>

應該是... wc.OpenWriteCompleted + =(發件人,E)=>

相關問題