1

我正在开发需要视频上传功能的应用程序。我为此使用 NSURLRequest,它对于长度小于 1 分钟的视频工作正常,但在视频很大时会导致问题。有人对此有任何想法吗???

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"----F00";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

fileData = [NSData dataWithContentsOfURL:[mediaDict objectForKey:UIImagePickerControllerMediaURL]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"data[file_name]\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat: @"Content-Type: %@\r\n\r\n",fileContentType] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:fileData];

[request setHTTPBody:body];

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

if (theConnection) {

    // Create the NSMutableData to hold the received data.

    // receivedData is an instance variable declared elsewhere.

    self.receivedData = [NSMutableData data];

} else {
    // Inform the user that the connection failed.
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];
    [spinnerView removeFromSuperview];
}

我真的很感谢你的帮助朋友。

4

1 回答 1

0

设置超时间隔并检查其是否工作

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
[request setHTTPMethod:@"POST"];
[request setTimeoutInterval:6000];
于 2013-08-13T08:59:53.250 回答