使用PhotoAlbum中的AFNetworking 3.0在服务器上上传视频?

时间:2016-03-21 07:04:54

标签: objective-c

自从过去2天以来我一直在尝试,但仍然没有得到具体的结果。使用AFNETWORKING 3.0在服务器端上传视频 所以我尝试下面的代码: 实际上我是从相册尝试然后在服务器端发送,但我得到了错误,如下所示:

AddressSanitizer debugger support is active. Memory error breakpoint has been installed and you can now use the 'memory history' command.
2016-03-21 12:28:40.851 Video_API[988:37226] Error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
2016-03-21 12:28:44.849 Video_API[988:37226] Video Selected ...... 
2016-03-21 12:28:44.849 Video_API[988:37226] Video URL is :file:///Users/mac/Library/Developer/CoreSimulator/Devices/258EB2FC-F56E-46A4-8960-177A2CF332B7/data/Containers/Data/Application/CBA626E9-4A62-4AFD-9B3A-CB76AE8AC0BE/tmp/trim.BB5FF527-6F44-4450-A84E-C81719A66D27.MOV
2016-03-21 12:28:44.850 Video_API[988:37226] +[__NSCFConstantString baseURL]: unrecognized selector sent to class 0x10474d0b8
2016-03-21 12:28:44.919 Video_API[988:37226] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[__NSCFConstantString baseURL]: unrecognized selector sent to class 0x10474d0b8'


- (IBAction)uploadVideo:(id)sender
{
    NSString *URLString = @"http://192.168.1.7/rakyesh/test/rest/rest/upload";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];



    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];


     NSDictionary *params = @{@"name":@"image"};
     [manager POST:URLString parameters:params progress:nil success:^(NSURLSessionDataTask *operation, id responseObject)

     {
         NSLog(@"Successfully Uploaded: %@", responseObject);

     } failure:^(NSURLSessionDataTask *operation, NSError *error)
    {
         NSLog(@"Error: %@", error);
     }];

    [self presentViewController:imagePicker animated:YES completion:NULL];

}

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"Video Selected ...... ");

    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    if([mediaType isEqualToString:(NSString *)kUTTypeMovie]==YES)
    {
        urlVideo=[info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"Video URL is :%@",urlVideo);
    }
    else if ([mediaType isEqualToString:(NSString *)kUTTypeImage]==YES)
    {
        NSDictionary *metaData=[info objectForKey:UIImagePickerControllerMediaMetadata];
        theImage =[info objectForKey:UIImagePickerControllerOriginalImage];

        NSLog(@"The Meta  Data is :%@",metaData);
        NSLog(@"The Image is  :%@",theImage);

    }

该服务器由php语言编写。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用以下代码:

httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@“你的php url here”];

NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
{
    [formData appendPartWithFileData:videoData name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime"];
}];


AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];

[operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) 
{

    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

}];

[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {NSLog(@"Success");} 
                                  failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@",  operation.responseString);}];
[operation start];