What is the correct way of sending large file through HTTP POST, without loading the whole file into ram?

前端 未结 3 1668
感情败类
感情败类 2021-02-01 08:45

I\'m currently working on an application for uploading large video files from the iPhone to a webservice through simple http post. As of right now, I build an NSURLRequest and p

3条回答
  •  失恋的感觉
    2021-02-01 09:37

    You can use NSInputStream on NSMutableURLRequest. For example:

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
    NSInputStream *stream = [[NSInputStream alloc] initWithFileAtPath:filePath];
    [request setHTTPBodyStream:stream];
    [request setHTTPMethod:@"POST"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
    }];
    

提交回复
热议问题