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:
- Got a "time out" error in
-URLSession:task:didCompleteWithError:
.
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?