Uploading multiple files with iOS SDK and deleting them locally when done

Sven

I have the following scenario: in my DocumentsDirectory there are user-created pdf-files. Now I want to upload them all to a webserver. When successfully done, i want to remove the file from the device.

My first problem is, that I do not know, when the upload is successful for a specific file. Additionally I get errors on some files.

At first some code:

-(void)uploadPDFs
{
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:docsDir error:nil];

    for (NSString *filename in contents) {
        if([filename.pathExtension isEqualToString:@"pdf"])
        {
            NSURL *docsDirURL = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:filename]];

            NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
            [config setTimeoutIntervalForResource:10];
            NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];

            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@/api/putPdf?name=%@&folder=", [defaults objectForKey:@"serverAdresse"], filename]];
            NSData *pdfData = [NSData dataWithContentsOfFile:docsDirURL.path];

            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
            [request setValue:@"application/pdf" forHTTPHeaderField:@"Content-Type"];
            [request setHTTPMethod:@"POST"];
            [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[pdfData length]] forHTTPHeaderField:@"Content-Length"];
            [request setTimeoutInterval:60];
            // UploadTask is a NSURLSessionUploadTask
            self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromData:pdfData];

            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

            [self.uploadTask resume];
        }
    }
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });

    if (!error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"NO ERROR");
            // CAN I GET HERE THE UPLOADED FILENAME?
    });
    } else {
        NSLog(@"ERROR: %@", error);
    }
}

The error I get especially when there are many uploads:

(-[ViewController URLSession:task:didCompleteWithError:]) (ViewController.m:4025) ERROR: Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fa3957028f0 {NSErrorFailingURLKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=, NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=http://192.168.100.150/api/putPdf?name=2015-03-26_AN_14.pdf&folder=}
Xcoder

Use the taskDescription property of an NSURLSessionUploadTask. ` For example;

task.taskDescription = filePath;

`

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Uploading/Deleting files from a database in Django

분류에서Dev

Send files with phpmailer before uploading them?

분류에서Dev

In your web app, when uploading files to S3, is it safe to upload them directly to S3 from the client side?

분류에서Dev

Uploading Multiple Web Files to Cloud Storage With Python

분류에서Dev

Multer not uploading files when I include logic in onFileUploadStart() - NodeJs

분류에서Dev

How to load multiple files from CDNs asynchronously (but execute them synchronously)?

분류에서Dev

How to select multiple files and copy them in Far Manager

분류에서Dev

Locally stream media files

분류에서Dev

Creating multiple UIPickerViews for textfields without putting them in accessory view in IOS

분류에서Dev

Drupal uploading files to server

분류에서Dev

CodeIgniter uploading files no file

분류에서Dev

CodeIgniter uploading files

분류에서Dev

How can Microsoft Word restrict files when saving them as PDF?

분류에서Dev

Android 4.4.2 not deleting files

분류에서Dev

Deleting older log files

분류에서Dev

ios JSON parsing error Error Domain = NSCocoaErrorDomain Code=3840 when uploading file and expecting response

분류에서Dev

AJAX callbacks when getting multiple XML files

분류에서Dev

Apahce Website - Uploading files and download

분류에서Dev

excessive creating and deleting files disadvantages

분류에서Dev

Finding and deleting files with a specific date

분류에서Dev

Reading text from multiple text files at the same time and splitting them into array of words

분류에서Dev

Moved files to trash when booted with Linux CD, can I recover them?

분류에서Dev

iOS >> Why isn't it Possible to Set UIControls Properties Directly in Code When Approaching Them via "sender"?

분류에서Dev

Uploading static websites using Ruby on Rails and displaying them

분류에서Dev

Primefaces 5 FileUploadEvent, not all files are uploading

분류에서Dev

Better way to create objects' array and then deleting them effectively

분류에서Dev

Nokia Asha SDK files

분류에서Dev

tmpfs — deleting files won't free the space

분류에서Dev

Deleting files except if the name contains a substring

Related 관련 기사

  1. 1

    Uploading/Deleting files from a database in Django

  2. 2

    Send files with phpmailer before uploading them?

  3. 3

    In your web app, when uploading files to S3, is it safe to upload them directly to S3 from the client side?

  4. 4

    Uploading Multiple Web Files to Cloud Storage With Python

  5. 5

    Multer not uploading files when I include logic in onFileUploadStart() - NodeJs

  6. 6

    How to load multiple files from CDNs asynchronously (but execute them synchronously)?

  7. 7

    How to select multiple files and copy them in Far Manager

  8. 8

    Locally stream media files

  9. 9

    Creating multiple UIPickerViews for textfields without putting them in accessory view in IOS

  10. 10

    Drupal uploading files to server

  11. 11

    CodeIgniter uploading files no file

  12. 12

    CodeIgniter uploading files

  13. 13

    How can Microsoft Word restrict files when saving them as PDF?

  14. 14

    Android 4.4.2 not deleting files

  15. 15

    Deleting older log files

  16. 16

    ios JSON parsing error Error Domain = NSCocoaErrorDomain Code=3840 when uploading file and expecting response

  17. 17

    AJAX callbacks when getting multiple XML files

  18. 18

    Apahce Website - Uploading files and download

  19. 19

    excessive creating and deleting files disadvantages

  20. 20

    Finding and deleting files with a specific date

  21. 21

    Reading text from multiple text files at the same time and splitting them into array of words

  22. 22

    Moved files to trash when booted with Linux CD, can I recover them?

  23. 23

    iOS >> Why isn't it Possible to Set UIControls Properties Directly in Code When Approaching Them via "sender"?

  24. 24

    Uploading static websites using Ruby on Rails and displaying them

  25. 25

    Primefaces 5 FileUploadEvent, not all files are uploading

  26. 26

    Better way to create objects' array and then deleting them effectively

  27. 27

    Nokia Asha SDK files

  28. 28

    tmpfs — deleting files won't free the space

  29. 29

    Deleting files except if the name contains a substring

뜨겁다태그

보관