zoukankan      html  css  js  c++  java
  • 下载文件win8mp3下载

    最近笔者几篇文章介绍了改下载文件的文章. 关联文章的地址

        前台代码:

        <Page.Resources>
            <Style TargetType="TextBlock">
                <Setter Property="FontSize" Value="27"/>
                <Setter Property="FontFamily" Value="宋体"/>
            </Style>
        </Page.Resources>

        
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

            <Grid.RowDefinitions>
                <RowDefinition Height="auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
           
            <StackPanel Grid.Row="0" Margin="15,8" Orientation="Horizontal">
                <TextBlock Text="输入下载URI:" VerticalAlignment="Center" />
                <TextBox x:Name="txtInputUri" Width="680"/>
                <Button x:Name="btnDown" Margin="38,0,0,0" VerticalAlignment="Center" Content="开始下载" Padding="17,5,17,5" FontSize="22" Click="onDownload_Click"/>
            </StackPanel>
           
            <StackPanel Grid.Row="1" Margin="20">
                <ProgressBar x:Name="probar" Maximum="100" Minimum="0" SmallChange="1" Width="700" HorizontalAlignment="Left" Foreground="Yellow"
                           Height="30" Margin="6,21,0,35"/>
                <TextBlock x:Name="tbMsg" Margin="6,8,0,0" />
            </StackPanel>

        
        </Grid>
    < /Page>

        后台代码:

        using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.Networking.BackgroundTransfer;
    using Windows.Storage;
    using Windows.Storage.Pickers;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;

        // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

        每日一道理
    漫漫人生路,谁都难免会遭遇各种失意或厄运。在凄风苦雨 惨雾愁云的考验面前,一个强者,是不会向命运低头的。风再冷,不会永远不息;雾再浓,不会经久不散。风息雾散,仍是阳光灿烂。

        namespace downLoad
    {
        /// <summary>
        /// 可用于自身或导航至 Frame 外部的空白页。
        /// </summary>
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }

            /// <summary>
            /// 在此页将要在 Frame 中显示时进行调用。
            /// </summary>
            /// <param name="e">描述如何拜访此页的事件数据。Parameter
            /// 属性通常用于配置页。</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
            }

            private async void onDownload_Click(object sender, RoutedEventArgs e)
            {
                // 判断是不是已输入下载URI 
               if (string.IsNullOrWhiteSpace(this.txtInputUri.Text)) return; 
                // 选择文件保存位置 
               FileSavePicker picker = new FileSavePicker(); 
                picker.FileTypeChoices.Add("MP3文件", new string[] { ".mp3" }); 
                StorageFile file = await picker.PickSaveFileAsync(); 
                if (file != null) 
                { 
                    // 实例化BackgroundDownloader 
                    BackgroundDownloader downloader = new BackgroundDownloader(); 
                    DownloadOperation operation = downloader.CreateDownload(new Uri(this.txtInputUri.Text), file);
                    // 进度 
                    Progress<DownloadOperation> progressDown = new Progress<DownloadOperation>(this.ProgressChanged);
                    // 开始下载 
                    btnDown.IsEnabled = false; 
                    var opresult = await operation.StartAsync().AsTask(progressDown);
                    btnDown.IsEnabled = true; 
                   // 判断下载结果 
                    switch (opresult.Progress.Status) 
                    { 
                        case BackgroundTransferStatus.Completed: 
                            tbMsg.Text = "下载已实现。"; 
                            break; 
                        case BackgroundTransferStatus.Error: 
                             tbMsg.Text = "下载进程中发生了错误。"; 
                            break; 
                        default: 
                            tbMsg.Text = "未知。"; 
                            break; 
                    } 
                } 
            }

                   /// <summary> 
            /// 呈文进度 
            /// </summary> 
            private async void ProgressChanged(DownloadOperation op) 
            { 
                var p = op.Progress; 
                double t = p.TotalBytesToReceive;//要下载总字节数 
                double d = p.BytesReceived;//已接收字节数 
                // 计算实现百分比 
                double pc = d / t * 100; 
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    { 
                        this.probar.Value = pc; 
                        this.tbMsg.Text = string.Format("已下载{0}字节/共{1}字节。", d.ToString("N0"), t.ToString("N0"));
                    }); 
            }

        }
    }

    文章结束给大家分享下程序员的一些笑话语录: 真正的程序员喜欢兼卖爆米花,他们利用CPU散发出的热量做爆米花,可以根据米花爆裂的速度听出正在运行什么程序。

  • 相关阅读:
    NHibernate中的Clear和Flush方法
    什么是POCO类
    node-vuecli 脚手架安装
    layui表单引入ueditor遇坑记
    PHP的九个超全局变量
    PHP的八个魔术常量
    PHP的七个数组指针函数
    TP6.0多应用模式隐藏路由中的应用名
    TP6.0中的密码验证逻辑、验证器的使用
    Vue 侦听器 watch
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3087227.html
Copyright © 2011-2022 走看看