xamarin.ios - Download Zip file in Xamarin iOS Native -


i have download zip file in xamarin ios. url - https://osdn.net/projects/sfnet_fotohound/downloads/sample-pictures/sample/sample-pictures.zip/

as hit url, download should start , same should saved in particular folder in documents directory.

how should implement same in xamarin native ios.

you can use nsurlsession download zip file.

firstly, should find real download link of download site. can refer this.

in chrome - run download normal - go menu - downloads - , should see direct link used.

actually, file link https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/sample/sample-pictures.zip.

now start code. create download task via nsurlsession:

        public void downloadtask()         {             // file link.             nsurl url = nsurl.fromstring("https://mirrors.netix.net/sourceforge/f/fo/fotohound/sample-pictures/sample/sample-pictures.zip");              // configure download session.             var config = nsurlsessionconfiguration.defaultsessionconfiguration;             nsurlsession session = nsurlsession.fromconfiguration(config, new simplesessiondelegate(), new nsoperationqueue());             var downloadtask = session.createdownloadtask(nsurlrequest.fromurl(url));              // start session.             downloadtask.resume();             console.writeline("start downloadtask!!!");         } 

configure callback didfinishdownloading:

    class simplesessiondelegate : nsurlsessiondownloaddelegate     {         public override void didfinishdownloading(nsurlsession session, nsurlsessiondownloadtask downloadtask, nsurl location)         {                 // configure destination path. here's saved /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 same name file in destination path.                  filemanager.remove(destinationpath, out error);                  // copy file tmp directory destination path. tmp file removed when delegate finishes.                 bool success = filemanager.copy(location.path, destinationpath, out error);                  if (!success)                 {                     console.writeline("error during copy: {0}", error.localizeddescription);                 }             }           }      } 

now file has been saved @ documents directory , named sample.zip.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -