下载Xamarin iOS本机中的Zip文件

下载Xamarin iOS本机中的Zip文件,ios,xamarin.ios,Ios,Xamarin.ios,我必须在Xamarin iOS中下载一个Zip文件。 URL-https://osdn.net/projects/sfnet_fotohound/downloads/sample-pictures/Sample/Sample-Pictures.zip/ 只要我点击这个URL,下载就应该开始,同样的内容应该保存在documents目录中的一个特定文件夹中 我应该如何在Xamarin原生iOS中实现相同的功能。您可以使用NSURLSession下载zip文件 首先,你应该找到这个下载站点的真正下载链

我必须在Xamarin iOS中下载一个Zip文件。 URL-
https://osdn.net/projects/sfnet_fotohound/downloads/sample-pictures/Sample/Sample-Pictures.zip/

只要我点击这个URL,下载就应该开始,同样的内容应该保存在documents目录中的一个特定文件夹中


我应该如何在Xamarin原生iOS中实现相同的功能。

您可以使用NSURLSession下载zip文件

首先,你应该找到这个下载站点的真正下载链接。 你可以参考

在Chrome中-正常运行下载-然后进入菜单-下载-您应该会看到使用的直接链接

实际上,您的文件链接是
https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/Sample/Sample-Pictures.zip

现在开始编码通过创建下载任务

        public void downloadTask()
        {
            // Your file link.
            NSUrl url = NSUrl.FromString("https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/Sample/Sample-Pictures.zip");

            // Configure your download session.
            var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
            NSUrlSession session = NSUrlSession.FromConfiguration(config, new SimpleSessionDelegate(), new NSOperationQueue());
            var downloadTask = session.CreateDownloadTask(NSUrlRequest.FromUrl(url));

            // Start the session.
            downloadTask.Resume();
            Console.WriteLine("Start DownloadTask!!!");
        }
配置回调
didfishdownloading

    class SimpleSessionDelegate : NSUrlSessionDownloadDelegate
    {
        public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
        {    
            // Configure your destination path. Here's saved to /Documents/ folder.
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);   
            var destinationPath = Path.Combine(documents, "Sample.zip");

            if (File.Exists(location.Path))
            {
                NSFileManager fileManager = NSFileManager.DefaultManager;
                NSError error;

                // Remove the same name file in destination path. 
                fileManager.Remove(destinationPath, out error);

                // Copy the file from the tmp directory to your destination path. The tmp file will be removed when this delegate finishes.
                bool success = fileManager.Copy(location.Path, destinationPath, out error);

                if (!success)
                {
                    Console.WriteLine("Error during the copy: {0}", error.LocalizedDescription);
                }
            }


        }
     }

现在该文件已保存在documents目录中,名为Sample.zip。

您可以使用NSURLSession下载zip文件

首先,你应该找到这个下载站点的真正下载链接。 你可以参考

在Chrome中-正常运行下载-然后进入菜单-下载-您应该会看到使用的直接链接

实际上,您的文件链接是
https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/Sample/Sample-Pictures.zip

现在开始编码通过创建下载任务

        public void downloadTask()
        {
            // Your file link.
            NSUrl url = NSUrl.FromString("https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/Sample/Sample-Pictures.zip");

            // Configure your download session.
            var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
            NSUrlSession session = NSUrlSession.FromConfiguration(config, new SimpleSessionDelegate(), new NSOperationQueue());
            var downloadTask = session.CreateDownloadTask(NSUrlRequest.FromUrl(url));

            // Start the session.
            downloadTask.Resume();
            Console.WriteLine("Start DownloadTask!!!");
        }
配置回调
didfishdownloading

    class SimpleSessionDelegate : NSUrlSessionDownloadDelegate
    {
        public override void DidFinishDownloading(NSUrlSession session, NSUrlSessionDownloadTask downloadTask, NSUrl location)
        {    
            // Configure your destination path. Here's saved to /Documents/ folder.
            var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);   
            var destinationPath = Path.Combine(documents, "Sample.zip");

            if (File.Exists(location.Path))
            {
                NSFileManager fileManager = NSFileManager.DefaultManager;
                NSError error;

                // Remove the same name file in destination path. 
                fileManager.Remove(destinationPath, out error);

                // Copy the file from the tmp directory to your destination path. The tmp file will be removed when this delegate finishes.
                bool success = fileManager.Copy(location.Path, destinationPath, out error);

                if (!success)
                {
                    Console.WriteLine("Error during the copy: {0}", error.LocalizedDescription);
                }
            }


        }
     }
现在,该文件已保存在documents目录中,并命名为Sample.zip