3

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?

4

1 回答 1

1

NSURLSessionUploadTask 将忽略 http 正文。

您可以将要上传的文件与已有的参数一起添加到 dataSend 对象中。然后将 dataSend 写入本地文件。最后使用该本地文件的 url 创建一个 uploadTask。

棘手的部分是准备该 dataSend 对象。您可以先尝试使用 NSURLConnection 发送一个 post 请求。然后使用上传任务将您的代码从 NSURLConnection 迁移到 NSURLSession。

于 2013-12-19T06:28:52.187 回答