【问题标题】:Why does the OnDownloadStart Method from Downloadlistener never gets called?为什么 Downloadlistener 的 OnDownloadStart 方法永远不会被调用?
【发布时间】:2014-08-23 11:40:45
【问题描述】:

也许你可以帮助我。:)

我正在用 C# 中的 Xamarin 开发一个 Android 应用程序。在我开发 ASP.NET 页面之前,现在应该在应用程序中实现。因此,我使用了 webview,一切正常。但我无法从服务器下载任何文件,这些文件是在 ASP.NET 页面中动态创建的。

我尝试实现一个下载监听器,但 OnDownloadStart 方法从未被调用。

这是我的代码的一些部分。

OnCreate 方法:

... some Code
// Init Webview
var webViewMain = FindViewById<WebView> (Resource.Id.webViewMain);
webViewMain.Settings.JavaScriptEnabled = true;
webViewMain.SetWebViewClient (new MyCustomWebViewClient ());
webViewMain.SetWebChromeClient (new WebChromeClient ());
webViewMain.LoadUrl (LoadUrl);

WebViewClient 类:

private class MyCustomWebViewClient: WebViewClient, IDownloadListener {

            ... some Code


            public void OnDownloadStart (string url, string userAgent, string contentDisposition, string mimetype, long contentLength)
            {
                Intent i = new Intent(Intent.ActionView);
                i.SetData(Android.Net.Uri.Parse("url"));
            }
}

ASP.NET 下载代码:

        ... some Code

        //ByteArray 
        var client = new WebClient();
        long length = dataFile.Length;
        byte[] buffer = client.DownloadData(dataFile.FullName);

        File.Delete(dataFile.FullName);

        // Send to User
        page.Response.Clear();
        page.Response.ClearHeaders();
        page.Response.ClearContent();
        page.Response.AddHeader("content-disposition", "attachment; filename=" + dataFile.Name);
        page.Response.AddHeader("Content-Lengt", length.ToString());
        page.Response.ContentType = "application/octet-stream";
        page.Response.BinaryWrite(buffer);

        page.Response.End();

非常感谢您的建议:)

【问题讨论】:

    标签: c# android asp.net download xamarin


    【解决方案1】:

    对于任何有兴趣的人,我没有解决问题,但我找到了一些解决方法。

    通过在我的 ASP.NET 页面中使用 Respone.Redirect() 命令到文件 url,我可以在 WebViewClient 的 ShouldOverrideUrlLoading 方法中处理下载。

     public override bool ShouldOverrideUrlLoading(WebView view, System.String url) {
                // handle different requests for different type of files
                // this example handles downloads requests for files
                // everything else the webview can handle normally
                if (url.EndsWith(".pdf") || url.EndsWith(".csv")) {
                    var source = Android.Net.Uri.Parse (url);
    
                    // Make a new request pointing to the .csv url
                    var request = new DownloadManager.Request(source);
    
                    request.AllowScanningByMediaScanner();
                    request.SetNotificationVisibility(Android.App.DownloadVisibility.VisibleNotifyCompleted);
    
                    // save the file in the "Downloads" folder of SDCARD
                    request.SetDestinationInExternalPublicDir(Android.OS.Environment.DirectoryDownloads, source.LastPathSegment);
                    // get download service and enqueue file
                    var manager = (DownloadManager) context.GetSystemService(Context.DownloadService);
                    manager.Enqueue(request);
                } else if(url.StartsWith("mailto:")){
                    var mt = MailTo.Parse(url);
                    var i = newEmailIntent(mt.To, mt.Subject, mt.Body, mt.Cc);
                    context.StartActivity(i);
                    view.Reload(); // Link von Email -> active style wieder entfernen.
                } else {
                    view.LoadUrl(url);
                }
                return true;                
            }
    

    我必须解决的另一个问题是,如何删除我动态创建的 csv 文件。因此我使用了这段代码:

    ASP.NET Schedule deletion of temporary files

    谢谢。

    【讨论】:

      【解决方案2】:

      我也找到了下载侦听器方法,因为它看起来比检查 .endswith(".pdf") 更干净,所以我尝试了它。

      使用 Xamarin 监听这些事件的方式是:

      webView.Download += (object sender, DownloadEventArgs de) => {
          if (de.Mimetype=="application/pdf") {
              var intent = new Intent (Intent.ActionView, Android.Net.Uri.Parse (de.Url));
              webView.Context.StartActivity (intent);
          }
      };
      

      【讨论】:

        【解决方案3】:

        有点晚了,但你的问题是你需要像这样添加监听器:

        var c = new MyCustomWebViewClient ();
        webViewMain.SetWebViewClient (c);
        webViewMain.SetDownloadListener(c);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-22
          • 2018-11-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多