I BELIEVE I CAN

加油,努力,奋斗!!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

The Ninth

Continue to study silverlight

Downloads with WebClient

public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();

            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

            // Uri need in ClientBin  This means we get file in web root folder
            client.OpenReadAsync(new Uri("..\\MT_B2_Anatomy.wmv", UriKind.Relative));
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                tbStatus.Text = string.Empty;
                myMedia.SetSource(e.Result);
                myMedia.Play();

            }
            catch (Exception exc)
            {
                tbStatus.Text = exc.Message;
            }
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            tbStatus.Text = String.Format("Persentage is {0}%", e.ProgressPercentage);
        }

 

<Grid x:Name="LayoutRoot" Background="White" VerticalAlignment="Center">
        <MediaElement x:Name="myMedia" Margin="10" />
        <TextBlock x:Name="tbStatus" Text="Not set yet" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>

 

Uploads with WebClient

We need add a handler to test like this:

public void ProcessRequest(HttpContext context)
        {
            System.IO.StreamReader sr =
                new StreamReader(context.Request.InputStream);

            string requestData = sr.ReadToEnd(); // ADD BreakPoint to test, look the data

            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

 

Upload String

private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();
            client.UploadProgressChanged += new UploadProgressChangedEventHandler(client_UploadProgressChanged);
            client.UploadStringCompleted += new UploadStringCompletedEventHandler(client_UploadStringCompleted);

            client.UploadStringAsync(new Uri("http://localhost:3615/Handler.ashx"), "POST", "Hello World");
        }

        void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            tbStatus.Text = "Completed";
        }

        void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            tbStatus.Text = String.Format("Persentage is {0}%", e.ProgressPercentage);
        }

Upload file content

private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            WebClient client = new WebClient();

            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == true)
            {
                client.OpenWriteCompleted += new OpenWriteCompletedEventHandler(client_OpenWriteCompleted);
                client.OpenWriteAsync(new Uri("http://localhost:3615/Handler.ashx", UriKind.Absolute), "POST",
                    dialog.File.OpenRead());
            }
        }


        void client_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
        {
            Stream inputStream = e.UserState as Stream;
            Stream outputStream = e.Result;

            byte[] buffer = new byte[4096];
            int bufferRead = 0;

            while ((bufferRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                outputStream.Write(buffer, 0, bufferRead);
            }
            inputStream.Close();
            outputStream.Close();
        }

 

posted on   朱小能  阅读(374)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示