我正在尝试使用CrossDownloadManager将文件下载到iOS,但是在任何地方都找不到该文件!经过调试,我发现它可以识别HTTP并“下载”该文件,但是找不到该文件。
我正在使用Ipad ios11。
我不知道是否有更简单的方法,但是这个插件似乎更可行。
我的代码:
CrossDownloadManager.Current.CollectionChanged += (sender, e) =>
System.Diagnostics.Debug.WriteLine(
"[DownloadManager] " + e.Action +
" -> New items: " + (e.NewItems?.Count ?? 0) +
" at " + e.NewStartingIndex +
" || Old items: " + (e.OldItems?.Count ?? 0) +
" at " + e.OldStartingIndex
);
var Url2 = "https://mysite/myfile.pdf"; //here i put my file, its ok.
DownloadFile(Url2);
}
public async void DownloadFile(String fileName)
{
await Task.Yield();
//await Navigation.PushPopupAsync(new DownloadingPage());
await Task.Run(() =>
{
var downloadManager = CrossDownloadManager.Current;
var file = downloadManager.CreateDownloadFile(fileName);
downloadManager.Start(file, true);
while (isDownloading)
{
isDownloading = IsDownloading(file);
}
});
//await Navigation.PopAllPopupAsync();
if (!isDownloading)
{
await DisplayAlert("File Status", "File Donwload","OK");
//DependencyService.Get<IToast>().ShowToast(“Baixou”);
}
}
public bool IsDownloading(IDownloadFile file)
{
if (file == null) return false;
switch (file.Status)
{
case DownloadFileStatus.INITIALIZED:
case DownloadFileStatus.PAUSED:
case DownloadFileStatus.PENDING:
case DownloadFileStatus.RUNNING:
return true;
case DownloadFileStatus.COMPLETED:
case DownloadFileStatus.CANCELED:
case DownloadFileStatus.FAILED:
return false;
default:
throw new ArgumentOutOfRangeException();
}
}
public void AbortDownloading()
{
CrossDownloadManager.Current.Abort(File);
}
在iOS部分中,我输入:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
Downloaded();
return base.FinishedLaunching(app, options);
}
public void Downloaded()
{
CrossDownloadManager.Current.PathNameForDownloadedFile = new System.Func<IDownloadFile, string>
(file => {
string fileName = (new NSUrl(file.Url, false)).LastPathComponent;
System.Diagnostics.Debug.WriteLine(Environment.SpecialFolder.Resources);
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Resources), fileName);
});
}
public override void HandleEventsForBackgroundUrl(UIApplication application, string sessionIdentifier, Action completionHandler)
{
CrossDownloadManager.BackgroundSessionCompletionHandler = completionHandler;
}