Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to download files from url and store in document folder [duplicate]

I created one application that has two page (first page for show list of data and second page for show detail data).

when click on any cell go to next page and in next page exists one button with name : DOWNLOAD that I want when I click on that button this file download and save in document folder. I dont know about it. please guide me that how download any file and store in document folder. I searching in internet but I dont understand about it.

please tell me with code that how downloaded any file with one button. Im sorry if I not good english.

like image 801
jacky Avatar asked May 06 '13 05:05

jacky


People also ask

How do I Copy files from one folder to another without duplicates?

All you have to do is hold down the Shift key while you click on no. It has the same effect as saying No To All which means that the copy process from that moment on will automatically select no if a duplicate file is found in the destination directory.

How do I save a duplicate file?

Go to the location of the document you want to duplicate. In the new file that opens, click File in the menu bar, then click Save As. Name the document as desired. If you want it to be saved in a different folder or drive, browse to that location before saving.

How do I duplicate a folder in File Explorer?

icon in the menu bar at the top of the File Explorer window. Alternatively, right-click the folder, select Show more options and then Copy. In Windows 10 and earlier versions, right-click the folder and select Copy, or click Edit and then Copy.


1 Answers

It is this simple my friend,

NSString *stringURL = @"http://www.somewhere.com/thefile.png";
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
  NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *documentsDirectory = [paths objectAtIndex:0];  

  NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
  [urlData writeToFile:filePath atomically:YES];
}

it advisable to execute the code in a separate thread.

EDIT 1: more info

1) for large file downloads,

-(IBAction) downloadButtonPressed:(id)sender;{
    //download the file in a seperate thread.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"Downloading Started");
        NSString *urlToDownload = @"http://www.somewhere.com/thefile.png";
        NSURL  *url = [NSURL URLWithString:urlToDownload];
        NSData *urlData = [NSData dataWithContentsOfURL:url];
        if ( urlData )
        {
            NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString  *documentsDirectory = [paths objectAtIndex:0];

            NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];

            //saving is done on main thread
            dispatch_async(dispatch_get_main_queue(), ^{
                [urlData writeToFile:filePath atomically:YES];
                NSLog(@"File Saved !");
            });
        }

    });

}
like image 139
Thilina Chamath Hewagama Avatar answered Oct 22 '22 15:10

Thilina Chamath Hewagama