【问题标题】:Background-transfer(Up-loader) progress issue for metro appsMetro 应用程序的后台传输(上传)进度问题
【发布时间】:2013-05-16 18:22:52
【问题描述】:

metro 应用程序的后台传输(上传)进度在一秒钟内给了我 100% 的大于 1 MB 的文件,如下所示:

*Running: c5593753-d155-425a-8e25-6b1f90bbff85
Progress: c5593753-d155-425a-8e25-6b1f90bbff85, Status: Running
 - Sent bytes: 131072 of 1310245 (10%), Received bytes: 0 of 0
Progress: c5593753-d155-425a-8e25-6b1f90bbff85, Status: Running
 - Sent bytes: 1310245 of 1310245 (100%), Received bytes: 0 of 0*

因此我的进度条也显示 100%,这当然是错误的。现在,上传完成一段时间后,我得到以下信息:

*Progress: c5593753-d155-425a-8e25-6b1f90bbff85, Status: Completed
 - Sent bytes: 1310245 of 1310245 (100%), Received bytes: 49 of 49
Completed: c5593753-d155-425a-8e25-6b1f90bbff85, Status Code: 200
 - Response updated; Header count: 9*

所以从某种意义上说,上传器正在工作,但这个错误再次导致文件实际上没有完成 100%。我已经测试了一个 10 MB 的文件,问题仍然存在。100% 的进度和“完成”状态仅在 3 分钟后出现。

我正在使用 ,http://code.msdn.microsoft.com/windowsapps/Background-Transfer-Sample-d7833f61 中提供的相同上传示例。

我使用的代码如下:

private async void StartUpload_Click(object sender, RoutedEventArgs e)
    {

       if (ApplicationView.Value == ApplicationViewState.Snapped && !ApplicationView.TryUnsnap())
        {
            rootPage.NotifyUser("File picker cannot be opened in snapped mode. Please unsnap first.", NotifyType.ErrorMessage);
            return;
        }

        FileOpenPicker picker = new FileOpenPicker();
        picker.FileTypeFilter.Add("*");
        StorageFile file = await picker.PickSingleFileAsync();

        Uri uri;


        string uploadFileURl = "https://dummy.com/Link1/Link2";


        if (!Uri.TryCreate(uploadFileURl, UriKind.RelativeOrAbsolute, out uri))
        {
            rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
            return;
        }

        if (file == null)
        {
            rootPage.NotifyUser("No file selected.", NotifyType.ErrorMessage);
            return;
        }

        BackgroundUploader uploader = new BackgroundUploader();

        uploader.Method = "POST";
        uploader.SetRequestHeader("uid", "1@test.com");
        uploader.SetRequestHeader("pwd","test");



        uploader.SetRequestHeader("file_name", file.Name);
        uploader.SetRequestHeader("p", "/");

        //just hard coding the particular file size for sample here
        Int64 size = 221390;
        uploader.SetRequestHeader("file_size", size.ToString());


        UploadOperation upload = uploader.CreateUpload(uri, file);



        // Attach progress and completion handlers.
        await HandleUploadAsync(upload, true);
    }

  private async Task HandleUploadAsync(UploadOperation upload, bool start)
    {

        try
        {
            LogStatus("Running: " + upload.Guid, NotifyType.StatusMessage);

            Progress<UploadOperation> progressCallback = new Progress<UploadOperation>(UploadProgress);
            if (start)
            {

                // Start the upload and attach a progress handler.
                await upload.StartAsync().AsTask(cts.Token,progressCallback);
            }
            else
            {
                // The upload was already running when the application started, re-attach the progress handler.
                await upload.AttachAsync().AsTask(cts.Token, progressCallback);
            }

            ResponseInformation response = upload.GetResponseInformation();

            LogStatus(String.Format("Completed: {0}, Status Code: {1}", upload.Guid, response.StatusCode),
                NotifyType.StatusMessage);
        }
        catch (TaskCanceledException)
        {
            LogStatus("Canceled: " + upload.Guid, NotifyType.StatusMessage);
        }
        catch (Exception ex)
        {

                throw; 

        }
    }

  private void UploadProgress(UploadOperation upload)
    {
        MarshalLog(String.Format("Progress: {0}, Status: {1}", upload.Guid, upload.Progress.Status));

        BackgroundUploadProgress progress = upload.Progress;

        double percentSent = 100;
        if (progress.TotalBytesToSend > 0)
        {
            percentSent = progress.BytesSent * 100 / progress.TotalBytesToSend;
        }

        //MarshalLog(String.Format(" - Sent bytes: {0} of {1} ({2}%)",
        //  progress.BytesSent, progress.TotalBytesToSend, percentSent));

        MarshalLog(String.Format(" - Sent bytes: {0} of {1} ({2}%), Received bytes: {3} of {4}",
            progress.BytesSent, progress.TotalBytesToSend, percentSent,
            progress.BytesReceived, progress.TotalBytesToReceive));

        if (progress.HasRestarted)
        {
            MarshalLog(" - Upload restarted");
        }

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

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

【问题讨论】:

    标签: microsoft-metro progress uploader background-transfer


    【解决方案1】:

    我相信这个问题现在已经解决了。这是一个简单的 DNS 设置问题。

    如果系统在 Proxy 之后:

    1. 转到 gpedit.msc -> 在计算机配置下 -> 管理模板 -> 网络 -> 网络隔离。
    2. 必须使用“IPaddress:Port”语法启用应用程序的 Internet 代理服务器。
    3. 运行命令行:netsh winhttp import proxy source=ie

    如果系统不在 Proxy 后面:

    请确保系统设置中的 DNS 服务器设置正确。这意味着,对于首选和辅助 DNS 服务器,必须提供实际的 DNS 服务器地址,而不是代理服务器地址。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-19
      • 2015-08-01
      • 2017-11-27
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 1970-01-01
      相关资源
      最近更新 更多