|
|
@@ -11,7 +11,10 @@
|
|
|
#import "AFHTTPSessionManager.h"
|
|
|
#import "frpUploadModel.h"
|
|
|
|
|
|
-@interface nasUploadManager ()
|
|
|
+#define Kboundary @"----WebKitFormBoundaryjv0UfA04ED44AhWx"
|
|
|
+#define KNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]
|
|
|
+
|
|
|
+@interface nasUploadManager ()<NSURLSessionDataDelegate>
|
|
|
@property(nonatomic,strong)AFHTTPSessionManager *uploadManager;
|
|
|
@property (nonatomic, strong) NSURLSessionDataTask *uploadTask;
|
|
|
@end
|
|
|
@@ -47,66 +50,160 @@
|
|
|
|
|
|
}
|
|
|
|
|
|
-//- (void)startUpload:(NSMutableDictionary *)params model:(uploadFileDataModel*)dataModel data:(NSData *)data success:(netWork_Success)success faild:(netWork_Faild)faildStr {
|
|
|
-//
|
|
|
-// NSString *urlString = ksharedAppDelegate.NASFileByBoxService;
|
|
|
-// urlString = [[NSString alloc] initWithFormat:@"%@uploadFile",urlString];
|
|
|
-// NSURL *URL = [NSURL URLWithString:urlString];
|
|
|
-// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
|
|
-// [request setHTTPMethod:@"POST"];
|
|
|
-//
|
|
|
-// // 将字典转换为JSON数据
|
|
|
-// NSError *error;
|
|
|
-// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
|
|
|
-//
|
|
|
-// if (jsonData) {
|
|
|
-// [request setHTTPBody:jsonData];
|
|
|
-//
|
|
|
-// // 设置请求头
|
|
|
-// [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
|
|
|
-// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
|
|
|
-//
|
|
|
-// // 现在你可以使用这个request进行网络请求
|
|
|
-// } else {
|
|
|
-// //处理错误
|
|
|
-// return;
|
|
|
-// }
|
|
|
-//
|
|
|
-// NSInteger position = 0;
|
|
|
-// if([[params allKeys] containsObject:@"position"]){
|
|
|
-// NSNumber *positionNumber = params[@"position"];
|
|
|
-// position = positionNumber.longValue;
|
|
|
-// }
|
|
|
+#pragma mark NSURLSessionDataDelegate
|
|
|
+
|
|
|
+// 1响应
|
|
|
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler{
|
|
|
+
|
|
|
+ completionHandler(NSURLSessionResponseAllow);
|
|
|
+}
|
|
|
+
|
|
|
+// 上传进度
|
|
|
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{
|
|
|
+
|
|
|
+ //每包发送的大小bytesSent,totalBytesSent已经上传了多少;totalBytesExpectedToSend总共要发送多少
|
|
|
+ // 32768 = 32KB
|
|
|
+ HLog(@"didSendBodyData: %lld--%lld-%lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 2 接收数据
|
|
|
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
|
|
|
+
|
|
|
+ NSDictionary *infoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
|
|
|
+ HLog(@"%@", infoDict);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+// 3 完成
|
|
|
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
|
|
|
+ HLog(@"task:%@---error:%@", task,error);
|
|
|
+}
|
|
|
+
|
|
|
+- (NSMutableData *)getBodyDataWithRequest:(NSMutableURLRequest *)request withModel:(uploadFileDataModel*)dataModel withData:(NSData*)data withPara:(NSMutableDictionary*)params{
|
|
|
+
|
|
|
+ //1 边界符号要配置请求头里面去
|
|
|
+ /*
|
|
|
+ multipart/form-data 是表单格式
|
|
|
+ charset=utf-8 是utf-8编码
|
|
|
+ bounary 是表单开头
|
|
|
+ */
|
|
|
+ [request setValue:[NSString stringWithFormat:@"multipart/form-data; charset=utf-8; boundary=%@", Kboundary] forHTTPHeaderField:@"Content-Type"];
|
|
|
+
|
|
|
+ /// body
|
|
|
+ NSMutableData *boydData = [NSMutableData data];
|
|
|
+ // 2.1 边界符号(开始边界)
|
|
|
+
|
|
|
+ //2.1.1 其它参数
|
|
|
+ NSMutableString *paraString = [NSMutableString new];
|
|
|
+ //[paraString appendFormat:@"--%@\r\n",Kboundary];//\n:换行 \n:切换到行首
|
|
|
+ for (NSString *key in params) {
|
|
|
+ NSString *value = params[key];
|
|
|
+ [paraString appendFormat:@"--%@\r\n",Kboundary];//\n:换行 \n:切换到行首
|
|
|
+ [paraString appendFormat:@"Content-Disposition: form-data; name=\"%@\"",key];
|
|
|
+ [paraString appendFormat:@"\r\n"];
|
|
|
+ [paraString appendFormat:@"%@\r\n",value];
|
|
|
+ }
|
|
|
+
|
|
|
+ [boydData appendData:[paraString dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+
|
|
|
+ // body每一个段内容以换行符作为结束标示
|
|
|
+ NSString *fileBeginBoundary = [NSString stringWithFormat:@"--%@\r\n", Kboundary];
|
|
|
+ [boydData appendData:[fileBeginBoundary dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+ // 2.2 属性配置 名字;key;类型
|
|
|
+
|
|
|
+ NSString *serverFileKey = @"image"; //key
|
|
|
+ //NSString *serverFileKey = @"file";
|
|
|
+ NSString *serverContentTypes = @"image/png"; //类型
|
|
|
+
|
|
|
+ if (dataModel.curUploadFileType == uploadFileTypeVideo) {
|
|
|
+ serverFileKey = @"video";
|
|
|
+ serverContentTypes = @"video/mp4";
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ NSString *serverFileName = dataModel.filename; //name
|
|
|
+
|
|
|
+
|
|
|
+ // filename已命名文件; name相当于一个key, 这个名字和服务器保持一致
|
|
|
+ /*
|
|
|
+ 理解key,表单发送给服务端,服务端拿到数据之后,可以将任务解析成一个字典了imageDict;图片数据会通过这个字典里面的name来获取图片(伪代码 image = imageDict[serverFileKey])
|
|
|
+ */
|
|
|
+ //2.3 拼接数据(创建一个字符串来拼装)
|
|
|
+ NSMutableString *string = [NSMutableString new];
|
|
|
+ [string appendFormat:@"Content-Disposition:form-data; name=\"%@\"; filename=\"%@\" ", serverFileKey, serverFileName];
|
|
|
+ //[string appendFormat:@"%@", KNewLine];
|
|
|
+ [string appendFormat:@"\r\n"];
|
|
|
+ [string appendFormat:@"Content-Type:%@", serverContentTypes];
|
|
|
+// [string appendFormat:@"%@", KNewLine];
|
|
|
+// [string appendFormat:@"%@", KNewLine];
|
|
|
+ [string appendFormat:@"\r\n"];
|
|
|
+ [string appendFormat:@"\r\n"];
|
|
|
+ [boydData appendData:[string dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+ // 2.3 拼接数据(拼接文件数据)
|
|
|
+ [boydData appendData:data];
|
|
|
+
|
|
|
+ // 2.4 边界符号 (结束边界)
|
|
|
+ NSString *fileEndBoundary = [NSString stringWithFormat:@"\r\n--%@", Kboundary];
|
|
|
+ [boydData appendData:[fileEndBoundary dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
+
|
|
|
+ return boydData;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)startUpload:(NSMutableDictionary *)params model:(uploadFileDataModel*)dataModel data:(NSData *)data success:(netWork_Success)success faild:(netWork_Faild)faildStr {
|
|
|
+
|
|
|
+ NSString *urlString = ksharedAppDelegate.NASFileByBoxService;
|
|
|
+ urlString = [[NSString alloc] initWithFormat:@"%@uploadFile",urlString];
|
|
|
+ NSURL *URL = [NSURL URLWithString:urlString];
|
|
|
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
|
|
|
+ [request setHTTPMethod:@"POST"];
|
|
|
+
|
|
|
+ //请求体
|
|
|
+ NSMutableData *bodyData = [self getBodyDataWithRequest:request withModel:dataModel withData:data withPara:params];
|
|
|
+
|
|
|
+ //设置请求体
|
|
|
+ [request setHTTPMethod:@"POST"];
|
|
|
+ [request setHTTPBody:bodyData];
|
|
|
+
|
|
|
+ //设置请求体长度
|
|
|
+ NSInteger length = [bodyData length];
|
|
|
+ [request setValue:[NSString stringWithFormat:@"%ld",length] forHTTPHeaderField:@"Content-Length"];
|
|
|
+ //设置 POST请求文件上传
|
|
|
+ [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",Kboundary] forHTTPHeaderField:@"Content-Type"];
|
|
|
+
|
|
|
+
|
|
|
+ //回话对象
|
|
|
+ NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
|
|
|
+
|
|
|
+ //请求task
|
|
|
+ /*
|
|
|
+ 第一个参数:请求对象
|
|
|
+ 第二个参数:传递是要上传的数据(请求体)
|
|
|
+ 第三个参数:
|
|
|
+ */
|
|
|
+ NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:nil completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+ //解析
|
|
|
+ HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
+ }];
|
|
|
+
|
|
|
+// NSString *filePath = [cachesFileManager getFilePathWithName:dataModel.filename type:uploadFileTypeVideo]; // 文件路径
|
|
|
+// NSURL *filePathUrl = [NSURL URLWithString:filePath];
|
|
|
//
|
|
|
-// // 这里应处理文件分块,此处为简化处理 验证这里OK
|
|
|
-// NSURLSessionUploadTask *uploadTask = [self.uploadManager uploadTaskWithRequest:request fromData:data progress:^(NSProgress * _Nonnull uploadProgress) {
|
|
|
-// HLog(@"上传222 Progress:--%@---%lld",uploadProgress,uploadProgress.completedUnitCount)
|
|
|
-// dataModel.didUploadBytes = position + (uploadProgress.completedUnitCount);
|
|
|
-// [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileRefreshNotification object:dataModel];
|
|
|
-// } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
|
|
|
-//
|
|
|
-// frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
-//
|
|
|
-// if(model && model.status == 0 && model.msg.length > 0){
|
|
|
-// success(responseObject);
|
|
|
-// HLog(@"%@ 上传完成 :--%@",dataModel.filename,responseObject)
|
|
|
-// }
|
|
|
-// else{
|
|
|
-// faildStr(error);
|
|
|
-// HLog(@"%@ 上传失败 :--%@",dataModel.filename,error)
|
|
|
-// }
|
|
|
+// NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:filePathUrl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
|
|
|
+// HLog(@"data string:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
|
|
|
// }];
|
|
|
-//
|
|
|
-// self.uploadTask = uploadTask;
|
|
|
-// [uploadTask resume];
|
|
|
-//
|
|
|
-//}
|
|
|
+ //执行Task
|
|
|
+ [uploadTask resume];
|
|
|
+}
|
|
|
|
|
|
- (void)nasUploadFileToFileServiceWithParams:(NSMutableDictionary *)params model:(uploadFileDataModel*)dataModel data:(NSData *)data success:(netWork_Success)success faild:(netWork_Faild)faildStr {
|
|
|
|
|
|
//test code
|
|
|
-// [self startUpload:params model:dataModel data:data success:success faild:faildStr];
|
|
|
-// return;
|
|
|
+ [self startUpload:params model:dataModel data:data success:success faild:faildStr];
|
|
|
+ return;
|
|
|
|
|
|
NSString *urlString = ksharedAppDelegate.NASFileByBoxService;
|
|
|
urlString = [[NSString alloc] initWithFormat:@"%@uploadFile",urlString];
|
|
|
@@ -146,6 +243,8 @@
|
|
|
|
|
|
faildStr(error);
|
|
|
}];
|
|
|
+
|
|
|
+ data = nil;
|
|
|
}
|
|
|
|
|
|
- (void)beginUploadDataBy:(uploadFileDataModel*)dataModel success:(netWork_Success)success faild:(netWork_Faild)faildStr
|
|
|
@@ -191,13 +290,14 @@
|
|
|
//NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:1 withModel:dataModel];
|
|
|
//[paraDict setObject:@0 forKey:@"isLast"];
|
|
|
|
|
|
- NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
+ __block NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
|
|
|
// NSData *videoData = [self cutVideoFileFunAtIndex:0 withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
|
|
|
|
|
|
[self nasUploadFileToFileServiceWithParams:paraDict model:dataModel data:videoData success:^(id _Nonnull responseObject) {
|
|
|
HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ videoData = nil;
|
|
|
success(responseObject);
|
|
|
|
|
|
// frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
@@ -215,6 +315,7 @@
|
|
|
// }
|
|
|
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
+ videoData = nil;
|
|
|
HLog(@"%@上传失败",dataModel.filename)
|
|
|
faildStr(error);
|
|
|
}];
|
|
|
@@ -234,11 +335,12 @@
|
|
|
[paraDict setObject:[NSNumber numberWithLong:position] forKey:@"position"];
|
|
|
|
|
|
//视频数据切片
|
|
|
- NSData *videoData = [self cutVideoFileFunAtIndex:position withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
+ __block NSData *videoData = [self cutVideoFileFunAtIndex:position withMaxLenght:MaxNasUploadPieceSzie withModel:dataModel];
|
|
|
|
|
|
KWeakSelf
|
|
|
[self nasUploadFileToFileServiceWithParams:paraDict model:dataModel data:videoData success:^(id _Nonnull responseObject) {
|
|
|
HLog(@"%@上传完成",dataModel.filename)
|
|
|
+ videoData = nil;
|
|
|
|
|
|
frpUploadModel *model = [[frpUploadModel alloc] initWithDictionary:responseObject error:nil];
|
|
|
if(model && model.msg){
|
|
|
@@ -255,12 +357,14 @@
|
|
|
}
|
|
|
}
|
|
|
else{
|
|
|
+ videoData = nil;
|
|
|
NSError *err = [NSError new];
|
|
|
faildStr(err);
|
|
|
}
|
|
|
|
|
|
} faild:^(NSError * _Nonnull error) {
|
|
|
HLog(@"%@上传失败---%@",dataModel.filename,error)
|
|
|
+ videoData = nil;
|
|
|
faildStr(error);
|
|
|
}];
|
|
|
}
|