gpt4 book ai didi

iphone - iOS 5 - AFNetworking - 上传视频

转载 作者:行者123 更新时间:2023-12-03 20:22:00 25 4
gpt4 key购买 nike

对于我的应用程序,用户将能够录制视频或从相册中选择视频,并通过 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];

我的 PHP 脚本运行良好,因为我能够通过 HTML 表单上传相同的视频。但是,上面的代码无法到达 setUploadProgressBlock。

是否有任何东西对任何人来说都是损坏的,或者我还缺少其他什么东西吗?

提前致谢

最佳答案

这是我用来解决问题的:

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];

关于iphone - iOS 5 - AFNetworking - 上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10505317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com