0

我不知道为什么,但我的处理程序没有被触发。(顺便说一下,我对 Silverlight 还是很陌生。)我正在关注这里的教程。我不确定我做错了什么......谢谢。[使用 Silverlight 3.0,VS2008]

主页.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);
        }
    }
}

接收器.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; }
    }
}
4

1 回答 1

1

我很笨...

该行... wc.OpenReadCompleted += (sender, e) =>

应该是... wc.OpenWriteCompleted += (sender, e) =>

于 2012-06-05T19:35:47.547 回答