コード例 #1
0
 private long CreateSystemDownloadTask(string url, Java.IO.File saveFile, string Referer, string title = "")
 {
     try
     {
         DownloadManager         downloadManager = (DownloadManager)Forms.Context.GetSystemService(Context.DownloadService);
         DownloadManager.Request request         = new DownloadManager.Request(Android.Net.Uri.Parse(url));
         if (title.Length != 0)
         {
             request.SetTitle(title);
         }
         //从360云盘返回的地址不能加Referer下载
         if (!url.Contains("360.cn"))
         {
             request.AddRequestHeader("Referer", Referer);
         }
         request.AddRequestHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36");
         request.SetDestinationUri(Android.Net.Uri.FromFile(saveFile));
         request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);
         long downloadId = downloadManager.Enqueue(request);
         return(downloadId);
     }
     catch (Exception ex)
     {
         return(0);
     }
 }
コード例 #2
0
        /// <summary>
        /// Starts download of given URL
        /// </summary>
        /// <param name="url">URL of file to download</param>
        /// <param name="filename">filename of file to download</param>
        private void StartDownload(string url, string filename)
        {
            var uri     = global::Android.Net.Uri.Parse(url);
            var request = new DownloadManager.Request(uri);

            request.SetTitle(filename);
            request.SetDescription("Downloading file...");

            var cookie = CookieManager.Instance.GetCookie(url);

            request.AddRequestHeader("Cookie", cookie);

            if (global::Android.OS.Build.VERSION.SdkInt < global::Android.OS.BuildVersionCodes.Q)
            {
#pragma warning disable CS0618 // Type or member is obsolete
                request.AllowScanningByMediaScanner();
#pragma warning restore CS0618 // Type or member is obsolete
            }
            else
            {
                request.SetDestinationInExternalPublicDir(
                    global::Android.OS.Environment.DirectoryDownloads,
                    filename);
            }

            request.SetNotificationVisibility(DownloadVisibility.VisibleNotifyCompleted);

            var downloadManager = (DownloadManager)this.Context.GetSystemService(Context.DownloadService);
            downloadManager.Enqueue(request);

            Toast.MakeText(this.Context, "Image download started...", ToastLength.Short).Show();
        }