5

从我的 iPhone 应用程序,我通过 HTTP 发布将视频上传到服务器。我成功发布了视频,即使我有 3 个问题(我必须解决这个问题)

1.如果网络中断了一段时间又回来了,如何重新上传?

2.如何在一个队列中上传多个文件

3.如果用户使用移动网络(3G),会不会有任何问题..目前我只测试WiFi(ipod)

我在这里添加我的文件发布代码..

"UploadClass.h"

@implementation UploadClass

      -(void)uploadVideoToServer:(NSDictionary *)bits file:(NSData *)file {



     appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    sharedclass = [SharedClass sharedInstance];




    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]];



    //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]];





    //video file

    [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];
      }







    //---when the end of element is found---
       -(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"];
            //show ui for daily posting failed!!!!!
            //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Upload Faild" message:@"Please try after sometime !!!"  delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            //[alert show];
            //[alert release];
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];


        }

       }




       }
4

0 回答 0