4

I'm doing a simple request in order to download PDF files and then write it to the file path. Some of the PDF's are getting directed to the fail block. The error code I'm getting is 200 and the error description is "transfer closed with 2231939 bytes remaining to read". Below is the code snippet.

    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:nil
                                         timeoutInterval:120];

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

    [downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (operation.response.statusCode != 200) {
            NSLog(@"Error code(S): %d",[operation.response statusCode]);
        }else{
            NSData *data = [[NSData alloc] initWithData:responseObject];

            [data writeToFile:filePath atomically:YES];
            NSLog(@"successful download to %@", filePath);

            [data release];
        }            
        [self fetchComplete:operation type:type];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error code: %d",[operation.response statusCode]);
        NSLog(@"error discreption: %@",[error localizedDescription]);
        [self fetchFailed:operation];
    }];
dammika
  • 115
  • 10
  • if only some of your files making this issue, may your server can reset connection? is there any other information on error description? – kocakmstf Feb 25 '15 at 11:33
  • check this http://stackoverflow.com/questions/1759956/curl-error-18-transfer-closed-with-outstanding-read-data-remaining – Mateusz Feb 25 '15 at 12:14

1 Answers1

3

Please, try this example. Hope this help!

// Step 1: create NSURL with full path to downloading file // for example try this: NSString *fileUrl = @"https://pbs.twimg.com/profile_images/2331579964/jrqzn4q29vwy4mors75s_400x400.png"; // And create NSURLRequest object with our URL

NSURL *URL = [NSURL URLWithString:fileUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

// Step 2: save downloading file's name // For example our fileName string is equal to 'jrqzn4q29vwy4mors75s_400x400.png'

NSString *fileName = [URL lastPathComponent];

// Step 3: create AFHTTPRequestOperation object with our request

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

// Step 4: set handling for answer from server and errors with request

[downloadRequest setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            // here we must create NSData object with received data...
            NSData *data = [[NSData alloc] initWithData:responseObject];
            // ... and save this object as file
            // Here 'pathToFile' must be path to directory 'Documents' on your device + filename, of course
            [data writeToFile:pathToFile atomically:YES];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"file downloading error : %@", [error localizedDescription]);
        }];

// Step 5: begin asynchronous download

[downloadRequest start];
bLacK hoLE
  • 781
  • 1
  • 7
  • 20