gpt4 book ai didi

ios - 使用 afnetworking 3.0 将应用程序视频上传到服务器

转载 作者:行者123 更新时间:2023-11-29 11:58:30 27 4
gpt4 key购买 nike

我将尝试使用 afnetworking 上传视频。我将在我的项目中拍摄一个视频而不是在服务器中上传。我只想在服务器中上传视频。

这是我的代码

- (IBAction)uplosd:(id)sender {
// name of the image
NSString *videoURL = [[NSBundle mainBundle] pathForResource:@"sample.mp4" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:videoURL]];
// convert your image into data

NSString *urlString = [NSString stringWithFormat:@"url"]; // enter your url to upload

// AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// allocate AFHTTPManager
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

[manager POST:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { // POST DATA USING MULTIPART CONTENT TYPE
[formData appendPartWithFileData:videoData
name:@"image"
fileName:@"video" mimeType:@"video/quicktime"]; // add image to formData
[formData appendPartWithFormData:[@"sdsd" dataUsingEncoding:NSUTF8StringEncoding]
name:@"key1"];
// add your other keys !!!

} progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Response: %@", responseObject); // Get response from the server



} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error); // Gives Error

}];

}

这里的 sample.mp4 是视频存储在我的项目中。请帮助我

最佳答案

1)

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSData *tempData = nil;

NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp4"];
NSData *data = [NSData dataWithContentsOfFile:path];



AFHTTPRequestOperation *operation = [manager POST:strRequestUrl parameters:pdictParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{

if (data)
{
[formData appendPartWithFileData:tempData name:@"video_file" fileName:[path lastPathComponent] mimeType:@"video/mp4"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject){
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];

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

NSLog(@"Upload Progress %lld",totalBytesWritten*100/totalBytesExpectedToWrite);


float fltProgress = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;

NSLog(@"%f",fltProgress);

}];
// [operation setInputStream:[NSInputStream inputStreamWithURL:[NSURL URLWithString:strRequestUrl]]];
[operation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
[operation pause];
}];

2)

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
NSString *path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mp4"];
NSURL *filePath = [NSURL fileURLWithPath:path];
[manager POST:strRequestUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"video" error:nil];
} progress:nil success:^(NSURLSessionDataTask *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

关于ios - 使用 afnetworking 3.0 将应用程序视频上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090976/

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