仅下载文件的一部分Windows 8.1 C#

时间:2015-01-29 08:03:06

标签: c# download windows-phone-8.1 windows-8.1 win-universal-app

我在Windows 8.1和Windows Phone 8.1(通用应用程序)上开发应用程序

有一个大约21 MB的文件,我只需要它的前50 KB数据。在此处使用Background Transfer示例:https://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61,当下载的字节数达到50时,我调用cts.Cancel,然后导出结果。但结果总是全文(是的,21 MB的文本)。

我该怎么做?

public static void DownloadProgress(DownloadOperation download)
    {
        //MarshalLog(String.Format(CultureInfo.CurrentCulture, "Progress: {0}, Status: {1}", download.Guid,
        //    download.Progress.Status));

        double percent = 100;
        if (download.Progress.TotalBytesToReceive > 0)
        {
            percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;

            if (StaticData.RssBreakHit && download.Progress.BytesReceived > 10240)
            {
                IInputStream inputStream = download.GetResultStreamAt(0);
                StreamReader reader = new StreamReader(inputStream.AsStreamForRead());
                string result = reader.ReadToEnd();
                StaticData.cts.Cancel();
                StaticData.cts.Dispose();
                Debug.WriteLine(result);
            }
        }

        //MarshalLog(String.Format(CultureInfo.CurrentCulture, " - Transfered bytes: {0} of {1}, {2}%",
        //    download.Progress.BytesReceived, download.Progress.TotalBytesToReceive, percent));

        if (download.Progress.HasRestarted)
        {
            //MarshalLog(" - Download restarted");
        }

        if (download.Progress.HasResponseChanged)
        {
            // We've received new response headers from the server.
            //MarshalLog(" - Response updated; Header count: " + download.GetResponseInformation().Headers.Count);

            // If you want to stream the response data this is a good time to start.
            // download.GetResultStreamAt(0);
        }

0 个答案:

没有答案