整体思路已经清楚,拿到视频资源,先转为mp4,写进沙盒,然后上传,上传成功后删除沙盒中的文件。

本地拍摄的视频,上传到服务器:

//视频转换为MP4
//转码操作...
_hud.mode = MBProgressHUDModeIndeterminate;
_hud.labelText = @"转码中...";
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:_filePathURL options:nil];
NSDateFormatter* formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"yyyyMMddHHmmss"];
_fileName = [NSString stringWithFormat:@"output-%@.mp4",[formater stringFromDate:[NSDate date]]];
_outfilePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@", _fileName];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset]; if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality])
{
MyLog(@"outPath = %@",_outfilePath);
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = [NSURL fileURLWithPath:_outfilePath];
exportSession.outputFileType = AVFileTypeMPEG4;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
if ([exportSession status] == AVAssetExportSessionStatusCompleted) {
MyLog(@"AVAssetExportSessionStatusCompleted---转换成功");
_filePath = _outfilePath;
_filePathURL = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@",_outfilePath]];
MyLog(@"转换完成_filePath = %@\n_filePathURL = %@",_filePath,_filePathURL);
//获取大小和长度
[self SetViewText];
[self uploadNetWorkWithParam:@{@"contenttype":@"application/octet-stream",@"discription":description}];
}else{
MyLog(@"转换失败,值为:%li,可能的原因:%@",(long)[exportSession status],[[exportSession error] localizedDescription]);
[_hud hide:YES];
[MyHelper showAlertWith:nil txt:@"转换失败,请重试"];
}
}]; } #pragma mark - 开始上传 -(void)uploadNetWorkWithParam:(NSDictionary*)dict
{
MyLog(@"开始上传_filePath = %@\n_filePathURL = %@",_filePath,_filePathURL);
AFHTTPRequestSerializer *ser=[[AFHTTPRequestSerializer alloc]init];
NSMutableURLRequest *request =
[ser multipartFormRequestWithMethod:@"POST"
URLString:[NSString stringWithFormat:@"%@%@",kBaseUrl,kVideoUploadUrl]
parameters:@{@"path":@"show",@"key":_key,@"discription":dict[@"discription"],@"isimage":@(_isImage)}
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:_filePathURL name:@"file" fileName:_fileName mimeType:dict[@"contenttype"] error:nil];
if (!_isImage) {
[formData appendPartWithFileURL:_path2Url name:@"tmp" fileName:@"tmp.PNG" mimeType:@"image/png" error:nil];
}
} error:nil];
//@"image/png" @"application/octet-stream" mimeType
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
MyLog(@"request = %@", request );
MyLog(@"response = %@", response );
MyLog(@"Error: %@", error );
[_hud hide:YES];
CXAlertView *alert=[[CXAlertView alloc]initWithTitle:NSLocalizedString(@"Warning", nil)
message:NSLocalizedString(@"Upload Failed",nil)
cancelButtonTitle:NSLocalizedString(@"Iknow", nil)];
alert.showBlurBackground = NO;
[alert show];
} else {
MyLog(@"%@ %@", response, responseObject);
NSDictionary *backDict=(NSDictionary *)responseObject;
if ([backDict[@"success"] boolValue] != NO) {
_hud.labelText = NSLocalizedString(@"Updating", nil);
[self UpdateResxDateWithDict:backDict discription:dict[@"discription"]];
[_hud hide:YES];
}else{
[_hud hide:YES];
[MyHelper showAlertWith:nil txt:backDict[@"msg"]];
}
}
[progress removeObserver:self
forKeyPath:@"fractionCompleted"
context:NULL];
}];
[progress addObserver:self
forKeyPath:@"fractionCompleted"
options:NSKeyValueObservingOptionNew
context:NULL];
[progress setUserInfoObject:@"someThing" forKey:@"Y.X."];
[uploadTask resume]; }

删除沙盒中的视频:

#pragma mark - 清除documents中的视频文件
-(void)ClearMovieFromDoucments{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL];
NSEnumerator *e = [contents objectEnumerator];
NSString *filename;
while ((filename = [e nextObject])) {
MyLog(@"%@",filename);
if ([filename isEqualToString:@"tmp.PNG"]) {
MyLog(@"删除%@",filename);
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
continue;
}
if ([[[filename pathExtension] lowercaseString] isEqualToString:@"mp4"]||
[[[filename pathExtension] lowercaseString] isEqualToString:@"mov"]||
[[[filename pathExtension] lowercaseString] isEqualToString:@"png"]) {
MyLog(@"删除%@",filename);
[fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL];
}
}
}

第五十三篇、OC利用AFN上传视频到服务器的更多相关文章

  1. 第五十四篇、OC利用AFN上传上传语音

    如果不是NSData就要想办法把语音文件转化为NSData,然后才能上传服务器. 首先我们拿到语音文件对应的NSData对象 NSData *voiceData = [message valueFor ...

  2. 《手把手教你》系列技巧篇(五十三)-java+ selenium自动化测试-上传文件-上篇(详细教程)

    1.简介 在实际工作中,我们进行web自动化的时候,文件上传是很常见的操作,例如上传用户头像,上传身份证信息等.所以宏哥打算按上传文件的分类对其进行一下讲解和分享. 2.为什么selenium没有提供 ...

  3. 利用 AFN 上传相册或拍照图片

    概述 自定义上传图片请求,自定义调取相册及拍照,方便多处使用时调用. 详细 代码下载:http://www.demodashi.com/demo/10718.html 由于项目中多处需要上传图片,我们 ...

  4. 利用bootstrap上传视频文件,mvc做后台处理

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...

  5. WPF上传文件到服务器

    利用WebClient 上传文件到服务器 创建一个空网站,创建一个UploadFile.aspx项, 服务器报500错误:检查文件保存路径是否存在,检查文件大小限制 protected void Pa ...

  6. 利用SecureCRT上传、下载文件(使用sz与rz命令),超实用!

    利用SecureCRT上传.下载文件(使用sz与rz命令),超实用! 文章来源:http://blog.csdn.net/dongqinliuzi/article/details/39623169 借 ...

  7. php 利用socket上传文件

    php 利用socket上传文件 张映 发表于 2010-06-02 分类目录: php 一,利用fsockopen来上传文件 以前我写过一篇关于socket通信原理的博文http://blog.51 ...

  8. 2.6 利用FTP上传所有文件

    利用FTP上传所有文件 import os,ftptools class UploadAll(ftptools.FtpTools): #继承上一篇写的Ftptools '''upload an ent ...

  9. SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET

    SAE利用storge上传文件 - myskies的专栏 - 博客频道 - CSDN.NET SAE利用storge上传文件

随机推荐

  1. Codeforces gym 100685 F. Flood bfs

    F. FloodTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100685/problem/F Desc ...

  2. spring-mvc整合freemarker并在ftl模版中显示服务端校验的错误信息,JSR303或者JSR349

    写法有多种,应该可以任意组合,最重要的是要引入spring.ftl 1.Bean里面的就不再多写了,来个简单就可以了 @NotEmpty(message="用户密码码不可为空") ...

  3. Android文件选择器的实例分享

    本文给大家讲解下Android文件选择器的使用.实际上就是获取用户在SD卡中选择的文件或文件夹的路径,这很像C#中的OpenFileDialog控件. 此实例的实现过程很简单,这样可以让大家快速的熟悉 ...

  4. [Angular 2] Understanding OpaqueToken

    When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...

  5. Javascript-获取URL请求参数

    function getUrlParam(){    var param = [], hash;    var url = window.location.href;//获取网页的url     va ...

  6. Gradient Boost Decision Tree(&Treelink)

    http://www.cnblogs.com/joneswood/archive/2012/03/04/2379615.html 1.      什么是Treelink Treelink是阿里集团内部 ...

  7. 实例源码--Android理财工具源码

      下载源码   技术要点: 1.Sqlite数据库的综合使用 2.控件的综合使用 3. 源码带详细的中文注释 ...... 详细介绍: 1. Sqlite数据库的综合使用 本套源码采用了Sqlite ...

  8. java_java 利用JAX-RS快速开发RESTful 服务

    JAX-RS(Java API for RESTful Web Services)同样也是JSR的一部分,详细规范定义见 https://jcp.org/en/jsr/detail?id=311 .从 ...

  9. mysql自动备份策略

    目标:每7天做一个完整备份,每天做一份binlog日志,第二周将之前的备份删除并产生新的完整备份和binlog日志,备份要求每天2:00自动完成 mysql 版本:mysql5.5 1.开启binlo ...

  10. 小白日记12:kali渗透测试之服务扫描(二)-SMB扫描

    SMB扫描 Server Message Block 协议.与其他标准的TCP/IP协议不同,SMB协议是一种复杂的协议,因为随着Windows计算机的开发,越来越多的功能被加入到协议中去了,很难区分 ...