我正在使用HTTP发布将视频发布到服务器。
如何在发布时停止此过程。 我得到了写入的字节,如果我想在按钮上停止发布,请点击我如何实现
我搜索了很多,但没有明确的想法
我在上传课程时添加了我的代码
@implementation UploadClass
-(void)uploadVideoToServer:(NSDictionary *)bits file:(NSData *)file {
appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *urlString =@"http://sampleurl.com/upload_video";
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//posting userid
[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_title\"\r\n\r\n%@", sharedclass.vdoTitle] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//posting video data
[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 didReceiveResponse:(NSURLResponse *) response
{
[webData setLength: 0];
}
-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
[webData appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
[webData release];
[conn release];
}
-(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
{
appDelegate.isVideoUploading=@"YES";
NSDictionary *uploadStatus=[[NSDictionary alloc]initWithObjectsAndKeys:
[NSString stringWithFormat:@"%i",totalBytesWritten],@"bytesWritten",
[NSString stringWithFormat:@"%i",totalBytesExpectedToWrite],@"totalBytes",
nil];
if (totalBytesWritten/totalBytesExpectedToWrite == 1) {
}
[uploadStatus release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSString *jsonString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding] ;
NSString *theXML = [[NSString alloc]
initWithBytes: [webData mutableBytes]
length:[webData length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
[theXML release];
if (xmlParser)
{
[xmlParser release];
}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];
[conn release];
[webData release];
}
-(void) parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *) qName attributes:(NSDictionary *) attributeDict
{
if( [elementName isEqualToString:@"status"])
{
if (!postStatus)
{
postStatus = [[NSMutableString alloc] init];
}
postStatusFound = YES;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (postStatusFound)
{
postStatusFound=NO;
[postStatus appendString: string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
sharedclass = [SharedClass sharedInstance];
if ([elementName isEqualToString:@"status"])
{
if([postStatus isEqualToString:@"true" ]){
appDelegate.isVideoUploading=@"NO";
[[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOADFINISHED" object:@"true"];
NSLog(@"upload finished;");
sharedclass.cameraBtn=YES;
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}else{
appDelegate.isVideoUploading=@"NO";
[[NSNotificationCenter defaultCenter] postNotificationName:@"UPLOADFINISHED" object:@"false"];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}
}
}
答案 0 :(得分:2)
将NSURLConnection
声明为类属性,您可以在班级或按钮点击的任何地方使用取消操作
-(IBAction)cancelPostOperation:(id)sender{
[self.conn cancel];
}