iOS 5 - AFNetworking - 上传视频

Wil*_*ill 2 iphone upload ios5 afnetworking

对于我的应用程序,用户将能够录制视频或从他们的相册中选择视频,并通过php脚本将视频文件上传到服务器.要将文件上传到服务器,我正在使用AFNetworking.我最初试图从相册上传视频,但由于我无法上传,我在主套装中添加了一个视频(我知道通过我为php脚本制作的html前端上传得很好).

代码是:

NSString *vidURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mov"];
    NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:vidURL]];

    NSLog(@"The test vid's url is %@.",vidURL);

    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL fileURLWithPath: @"http://www.mywebsite.com"]];

    NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload.php" parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData) 
    {
        [formData appendPartWithFileData:videoData name:@"file" fileName:@"test.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 start];
Run Code Online (Sandbox Code Playgroud)

我的PHP脚本运行正常,因为我可以通过HTML表单上传相同的视频.但是,上面的代码无法访问setUploadProgressBlock.

有什么东西对任何人来说都是破碎的,还是有其他我想念的东西?

提前致谢

Wil*_*ill 5

这是我用来解决问题的方法:

httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.mysite.com"]];

    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];
Run Code Online (Sandbox Code Playgroud)