2014-07-02 178 views
1

也许你可以帮我。:)为什么Downloadlistener的OnDownloadStart方法永远不会被调用?

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

我试图实现downloadlistener,但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(); 

许多感谢您的建议:)

回答

0

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

通过在我的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

感谢。

0

我发现了下载监听器的方法,因为它看起来比检查.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); 
    } 
}; 
0

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

var c = new MyCustomWebViewClient(); 
webViewMain.SetWebViewClient (c); 
webViewMain.SetDownloadListener(c); 
相关问题