上传视频超过10 MB的问题?

时间:2011-01-10 09:10:19

标签: iphone

我尝试上传超过10 MB的视频,当我通过ASIHTTPRequest发送它然后它开始连接但没有将该电影上传到网络

2 个答案:

答案 0 :(得分:2)

可能是由于服务器端的大小限制

-edit-

使用NSURLConnection上传以下请求(如果您使用多部分表单数据,通常就是这种情况):

// some random value

 NSString *boundary = @"0xKhTmLbOuNdArY---ThIs_Is_mY_BoUnDaRyY---pqo";


 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
 [request setHTTPMethod:@"POST"];


 NSString *theContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
 [request setValue:theContentType forHTTPHeaderField:@"Content-type"];

 NSData *theData = data;// here use your movie data

 //adding the body:
 NSMutableData *postBody = [NSMutableData data];

// data part 

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"content\"; filename=\"dummy%i.mp4\"\r\n",[[NSDate date]timeIntervalSince1970]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n",contentType] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[postBody appendData:theData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];


// final boundary

[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

 [request setHTTPBody:postBody];
 [request setValue:param forHTTPHeaderField:@"Parameter"];

答案 1 :(得分:2)

我使用这种方式解决了这个问题,基本上由于服务器上传限制而产生错误

-(void)uploadeVideoClicked{

    if (_picker) {
        [_picker dismissModalViewControllerAnimated:NO];
        [_picker.view removeFromSuperview];
        [_picker release];
    }
    if (_popover) {
        [_popover dismissPopoverAnimated:NO];
        [_popover release];
    }
    _picker = [[[UIImagePickerController alloc] init] retain];
    _picker.delegate = self;
    _picker.editing = NO;
    _picker.allowsEditing=NO;
    _picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    _picker.wantsFullScreenLayout = YES;

    _picker.videoQuality =UIImagePickerControllerQualityTypeLow;
    _picker.mediaTypes =[NSArray arrayWithObject:(NSString *)kUTTypeMovie];
    _popover = [[[UIPopoverController alloc] initWithContentViewController:_picker] retain];
    [_popover setDelegate:self];
    [_popover presentPopoverFromRect:infoButton.frame
                              inView:self.view
            permittedArrowDirections:UIPopoverArrowDirectionAny
                            animated:YES];
}
#pragma mark -----------------------------
#pragma mark Image Picker Delegate Methods

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{ 

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:@"public.image"]){


UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"" message:@"You Select a image Please select Movie" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [myAlertView show];

        [myAlertView release];

    }
    else if ([mediaType isEqualToString:@"public.movie"]){

        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        //NSLog(@"found a video");
        if (webData) {
            [webData release];
            webData = nil;
        }

    webData = [[NSMutableData alloc]initWithContentsOfURL:videoURL];

       // webData = [NSData dataWithContentsOfURL:videoURL];

        spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

        spinner.center = self.view.center;

        [self.view addSubview:spinner];

        [spinner startAnimating];

    [self performSelector:@selector(uploadVideo) withObject:nil afterDelay:.001];

    }

    [_picker dismissModalViewControllerAnimated:YES];
    [_picker.view removeFromSuperview];
    [_picker release];
    _picker = nil;
    [_popover dismissPopoverAnimated:YES];
    [_popover release];

}
-(void)uploadVideo
{

NSString *url = [NSString stringWithFormat:@"http://example.com/uploadvideoxml.php"];

    networkQueue = [[ASINetworkQueue alloc] init];

    [networkQueue cancelAllOperations];

    [networkQueue setShowAccurateProgress:YES];

    [videoUploadProgressView setHidden:NO];

    progressView.center = self.view.center;

    [self.view addSubview:progressView];

    [networkQueue setUploadProgressDelegate:videoUploadProgressView];

    [networkQueue setDelegate:self];

    [networkQueue setRequestDidFinishSelector:@selector(requestFinished:)];

    [networkQueue setRequestDidFailSelector: @selector(requestFailed:)];


    request= [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:url]] ;

    [request setPostValue:[[mAppDelegate.userInfoArray objectAtIndex:1]valueForKey:@"user_id"] forKey:@"user_id"];

    [request addRequestHeader:@"Content-Type"
                        value:@"multipart/form-data;boundary=---------------------------1842378953296356978857151853"];


    NSDate *date1=[NSDate date];

    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];

    [formatter1 setDateFormat:@"hh:mm"];

    NSString *valuestr = [formatter1 stringFromDate:date1];

    ////NSLog(@"%@",valuestr);

    [formatter1 release];

    NSString *moviename = [NSString stringWithFormat:@"WakeUUUP_%d_%@.mov",[videolistArray count],valuestr];
    if(webData != nil){
        [request setFile:webData withFileName:moviename andContentType:@"video/quicktime" forKey:@"userfile"];
    }

    [request setTimeOutSeconds:5000];
    //NSLog(@"%@",request);
    [networkQueue addOperation:request];
    [networkQueue go];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [_picker dismissModalViewControllerAnimated:YES];
    [_picker.view removeFromSuperview];
    [_picker release];
    _picker = nil;
    [_popover dismissPopoverAnimated:YES];
    [_popover release];
}

//for Ipad UIPopoverController if there is a cancel when the user click outside the popover
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    [_picker dismissModalViewControllerAnimated:YES];
    [_picker.view removeFromSuperview];
    [_picker release];
    _picker = nil;
}

#pragma mark -------------------------
#pragma mark ASIHTTPREQUEST Delegate 
- (void)requestFailed:(ASIHTTPRequest *)req
{

    [progressView removeFromSuperview];
    [videoUploadProgressView setHidden:YES];
    ////NSLog(@"Request failed: %@",[req error]);
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Failed to Post Item" message:[[req error] description] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil, nil];
    [errorAlert show];
    [errorAlert release];
    [networkQueue release];
    [request release];
}
- (void)requestFinished:(ASIHTTPRequest *)req
{
    [networkQueue release];
    [request release];
    [progressView removeFromSuperview];
    [videoUploadProgressView setHidden:YES];

    [[Parsing sharedInstance] assignSender:self];
    NSMutableArray *resultArray =[[[NSMutableArray alloc]initWithArray:[[Parsing sharedInstance]startparsingforfun:[req responseData]]]autorelease];

    NSLog(@"Response was: %@",resultArray);
    if (spinner) {
        [spinner stopAnimating];
        [spinner removeFromSuperview];
        [spinner release];
        spinner = nil;
    }
    if ([[[resultArray objectAtIndex:1]objectForKey:@"Transaction"]isEqualToString:@"uploadvideoxml"]) {
        if ([[[resultArray objectAtIndex:1]objectForKey:@"success"]isEqualToString:@"true"]) {
            [self viewWillAppear:YES];

        }else {

            UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Failed to Update  Profile" message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil, nil];
            [errorAlert show];
            [errorAlert release];
        }

    }
}