1

I have mp3 file in server url. Then i want to download the file store for later access. I'm using AFNetworking to download the file from url. I have been using AFHTTPRequestOperation for access the url, but i'm getting the url path in NSLog, but i didn't get file downloaded or not. The download option is not executing.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
            [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.detailsofmylife.net/wp-content/uploads/2010/02/Ellie-Goulding-Starry-Eyed-Live-Lounge-@-Radio-1.mp3"]];

        NSLog(@"The video url is %@", request);


        AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

        operation.outputStream = [NSOutputStream outputStreamToFileAtPath:dataPath append:NO];

        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]);
            NSLog(@"THE RESPONSE: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error1) {
            NSLog(@"Error: %@", error1);
        }];

        [operation start];
user3743552
  • 143
  • 3
  • 13
  • Your post is garbled. You said: " I have been using AFHTTPRequestOperation for access the url, but i'm getting the url path in NSLog, but i didn't get file downloaded or not." What does that mean? Are you seeing log statements in the console? if so, what is the exact text you are seeing in the console log? Have you tried setting breakpoints and stepping through your code? – Duncan C Jun 20 '14 at 13:58

1 Answers1

0

You could try to download your mp3 file like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"images"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

[manager.requestSerializer setValue:@"application/x-www-form-urlencoded"
                  forHTTPHeaderField:@"Content-Type"];

[manager setResponseSerializer:[AFJSONResponseSerializer serializer]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"audio/mpeg", nil];

[manager GET:@"http://www.detailsofmylife.net/wp-content/uploads/2010/02/Ellie-Goulding-Starry-Eyed-Live-Lounge-@-Radio-1.mp3"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [operation.responseData writeToFile:[dataPath stringByAppendingPathComponent:@"file.mp3"] atomically:YES];
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@", error);
     }];

Hope this helps!

Alex
  • 481
  • 1
  • 5
  • 19
  • How to check my file is saved or not. I haven't get anything in NSLog. The images is my folder name of resource. – user3743552 Jun 20 '14 at 12:30
  • I used below code for check the saved file. But i didn't get any log NSLog(@"Successfully downloaded file to %@", [NSURL fileURLWithPath:dataPath]); NSLog(@"THE RESPONSE: %@", responseObject); – user3743552 Jun 20 '14 at 12:31