HTTP投稿を使用してサーバーにビデオをアップロードしています。
私は次のコードを使用しています
NSString *urlString =@"http://sampleurl.com/upload_video";
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%@", appDelegate.userid] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", @"a.mov"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:file]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}
これは、書き込まれたバイト数と合計バイト数を調べるために使用している関数です。
-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:( NSInteger)totalBytesExpectedToWrite{
NSLog(@"------%i------%i-----------",totalBytesWritten ,totalBytesExpectedToWrite);
NSDictionary *uploadStatus=[[NSDictionary alloc]initWithObjectsAndKeys:
[NSString stringWithFormat:@"%i",totalBytesWritten],@"bytesWritten",
[NSString stringWithFormat:@"%i",totalBytesExpectedToWrite],@"totalBytes",
nil];
[uploadStatus release];
}
アプリがバックグラウンドに戻るとアップロードが一時停止され、フォアグラウンドに戻るとアップロードが再開されます 私の問題は...長いビデオをアップロードすると時間がかかり、電話が自動的にロックされます。その後、アップロードプロセスが機能しません。アップロードがスタックし、アプリを再起動する必要があります
なぜそれが起こっているのか..事前にこの感謝をクリアするのを手伝ってください