Pergunta

I want to upload a big file using NSURLSessionUploadTask with the fileUrl API. The following code is what I wrote to upload a big file. I found these problems:

  1. Got a "time out" error in -URLSession:task:didCompleteWithError:.

  2. No http body found in the sent package (by using wireshark). Here is my code:

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];   
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
    
    NSURL *url = [My URL];
    fileName = [[DBList objectAtIndex:fileIndex] objectForKey:@"title"];
    NSString *boundary = [self getBoundaryStr];
    
    NSMutableData *dataSend = [[NSMutableData alloc] init];
    [dataSend appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"target_path"] dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[[NSString stringWithFormat:@"/%@",[NSString stringWithFormat:@"%@/%@/%@",UploaderController.getDestination,APP_UPLOADER,[Functions getDateString]]] dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_path\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
    [dataSend appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
    [request setValue:[@"authtok=" stringByAppendingString:[broker getNasAuthtok]] forHTTPHeaderField:@"Cookie"];
        [request setHTTPBody:dataSend];
    
    NSString *tempFile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"uploadFile"];
    NSURL *uploadfileURL = [[NSURL alloc] initFileURLWithPath:tempFile];
    NSURLSessionUploadTask *sessionUploadTask = [session uploadTaskWithRequest:request fromFile:uploadfileURL];
    [sessionUploadTask resume];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    

anyone know where did I miss? Thank you very much!!

I am wondering about that the fileURL means a file including some post data and the uploaded file, am I right?

or just a file like an video for upload?

Foi útil?

Solução

NSURLSessionUploadTask will ignore the http body.

You can add the file that you want to upload to your dataSend object along with the parameter that you already have. Then write dataSend to a local file. Finally create an uploadTask with the url of that local file.

The tricky part is to prepare that dataSend object. you can first try using NSURLConnection to send a post request. then migrate your code from NSURLConnection to NSURLSession using upload task.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top