视频从iphone应用上传到服务器

时间:2013-02-20 10:01:35

标签: iphone ios http video file-upload

从我的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];


        }

       }




       }

0 个答案:

没有答案