如何使用AFNetworking 3.0(Obj-C)将大型视频上传到服务器(php)

时间:2017-01-29 17:20:21

标签: php ios xcode upload afnetworking

我正在尝试使用 AFNetworking 3.0 创建一个简单的视频上传任务。我知道如何上传较小的视频,我需要能够上传(流)大文件(更长的视频)。以下是我目前用于上传的代码。另外,我在服务器端使用PHP,是否需要进行任何特殊更改?

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"http://v.afrazsiddiqui.com/?cmd=post&id=%@",[UDM returnValueforkey:@"userid"]] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileURL:self.videoURL name:@"file" fileName:@"filename.mov" mimeType:@"video/quicktime" error:nil];
} error:nil];


AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat done = self.view.frame.size.width*uploadProgress.fractionCompleted;
        [self updateProgressWithPercentage:done];
    });
}
completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
    if (error) {
        errorUploading=YES;
    }
    else {
        JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",JSON);
        if ([[JSON valueForKey:@"Success"] isEqualToString:@"true"]) {
            [self reloadProfile];
        }
        else if ([[JSON valueForKey:@"Error"] isEqualToString:@"Upload"]) {
            if (!errorUploading) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Vlog Error" message:@"An error occurred when trying to post your vlog." delegate:self cancelButtonTitle:@"No, Thanks" otherButtonTitles:@"Retry", nil];
                [alert show];
            }
        }
    }
}];
[uploadTask resume];

0 个答案:

没有答案