Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified T8NetworkKitDemo/.DS_Store
Binary file not shown.
Binary file modified T8NetworkKitDemo/Pods/.DS_Store
Binary file not shown.
9 changes: 5 additions & 4 deletions T8NetworkKitDemo/T8NetWorkKit/T8NetworkBaseService.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ typedef void(^RequestProgressBlock)(NSUInteger bytesWritten, long long totalByte
@property (nonatomic, assign) FileModelType type;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, strong) NSData *data;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *fileName;
@property (nonatomic, copy) NSString *mimeType;
@end

/**
* 文件模型数据
* 文件模型数组
*/
@interface T8FileModelArray : NSObject
@property (nonatomic, strong) NSArray *fileModelArray;
@property (nonatomic, strong) NSArray<T8FileModel *> *fileModelArray;

@end

Expand Down Expand Up @@ -85,7 +86,7 @@ typedef void(^RequestProgressBlock)(NSUInteger bytesWritten, long long totalByte
* @param progressBlock 上传进度回调方法
* @param completBlock 上传成功回调方法
*/
+ (void)uploadFile:(T8FileModel *)fileModel urlPath:(NSString *)strUrlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completBlock:(RequestComplete)completBlock;
+ (void)uploadFile:(T8FileModel *)fileModel urlPath:(NSString *)strUrlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completeBlock:(RequestComplete)completeBlock;

/**
* 上传一组文件
Expand All @@ -96,7 +97,7 @@ typedef void(^RequestProgressBlock)(NSUInteger bytesWritten, long long totalByte
* @param progressBlock 上传进度回调方法
* @param completBlock 上传完成回调方法
*/
+ (void)uploadFiles:(T8FileModelArray *)files urlPath:(NSString *)strUrlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completBlock:(RequestComplete)completBlock;
+ (void)uploadFiles:(T8FileModelArray *)files urlPath:(NSString *)strUrlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completeBlock:(RequestComplete)completeBlock;

@end

33 changes: 18 additions & 15 deletions T8NetworkKitDemo/T8NetWorkKit/T8NetworkBaseService.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,57 +59,59 @@ + (void)sendRequestUrlPath:(NSString *)strUrlPath httpMethod:(HttpMethod)httpMet
[dataTask resume];
}

+ (void)uploadFile:(T8FileModel *)fileModel urlPath:(NSString *)urlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completBlock:(RequestComplete)completBlock
+ (void)uploadFile:(T8FileModel *)fileModel urlPath:(NSString *)urlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completeBlock:(RequestComplete)completeBlock
{
AFHTTPSessionManager *manager = [self shareHttpManager];
NSString *urlStr = [self getRequestUrl:urlPath];

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[self getRequestUrl:urlStr] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (fileModel.type == FileModelData) {
if (fileModel.data) {
[formData appendPartWithFileData:fileModel.data name:@"" fileName:@"" mimeType:fileModel.mimeType];
[formData appendPartWithFileData:fileModel.data name:fileModel.name fileName:fileModel.fileName mimeType:fileModel.mimeType];
}
}else if (fileModel.type == FileModelPath){
if (fileModel.path.length > 0) {
[formData appendPartWithFileURL:[NSURL URLWithString:fileModel.path] name:@"" fileName:@"" mimeType:fileModel.mimeType error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:fileModel.path] name:fileModel.name fileName:fileModel.fileName mimeType:fileModel.mimeType error:nil];
NSLog(@"formData = %@", formData);
}
}
} error:nil];

if (T8RequestHeaderBlock) {
T8RequestHeaderBlock(request);
}

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

// typedef void(^RequestProgressBlock)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite);
progressBlock(uploadProgress.completedUnitCount, uploadProgress.totalUnitCount, 0);
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
T8NetworkError *_error = [T8NetworkError errorWithNSError:error];
completeBlock(RequestStatusFailure, nil, _error);
} else {
NSLog(@"%@ %@", response, responseObject);
completeBlock(RequestStatusSuccess, responseObject, nil);
}
}];
[uploadTask resume];
}



+ (void)uploadFiles:(T8FileModelArray *)files urlPath:(NSString *)urlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completBlock:(RequestComplete)completBlock;
+ (void)uploadFiles:(T8FileModelArray *)files urlPath:(NSString *)urlPath params:(NSMutableDictionary *)params progressBlock:(RequestProgressBlock)progressBlock completeBlock:(RequestComplete)completeBlock;
{
NSArray *fileModelArray = files.fileModelArray;

AFHTTPSessionManager *manager = [self shareHttpManager];
NSString *urlStr = [self getRequestUrl:urlPath];
NSArray *fileModelArray = files.fileModelArray;

NSMutableURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:[self getRequestUrl:urlStr] parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (T8FileModel *fileModel in fileModelArray) {
if (fileModel.type == FileModelData) {
if (fileModel.data) {
[formData appendPartWithFileData:fileModel.data name:@"" fileName:@"" mimeType:fileModel.mimeType];
[formData appendPartWithFileData:fileModel.data name:fileModel.name fileName:fileModel.fileName mimeType:fileModel.mimeType];
}
}else if (fileModel.type == FileModelPath){
if (fileModel.path.length > 0) {
[formData appendPartWithFileURL:[NSURL URLWithString:fileModel.path] name:@"" fileName:@"" mimeType:fileModel.mimeType error:nil];
[formData appendPartWithFileURL:[NSURL fileURLWithPath:fileModel.path] name:fileModel.name fileName:fileModel.fileName mimeType:fileModel.mimeType error:nil];
}
}
}
Expand All @@ -120,12 +122,13 @@ + (void)uploadFiles:(T8FileModelArray *)files urlPath:(NSString *)urlPath params
}

NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {

progressBlock(uploadProgress.completedUnitCount, uploadProgress.totalUnitCount, 0);
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
T8NetworkError *_error = [T8NetworkError errorWithNSError:error];
completeBlock(RequestStatusFailure, nil, _error);
} else {
NSLog(@"%@ %@", response, responseObject);
completeBlock(RequestStatusSuccess, responseObject, nil);
}
}];
[uploadTask resume];
Expand Down
4 changes: 4 additions & 0 deletions T8NetworkKitDemo/T8NetworkKitDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
3FB896DB94C8572FB4C92510 /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 042B099277402A303BA96B6C /* libPods.a */; };
75DE4EDA1CA01FBA00197A05 /* abc.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 75DE4ED91CA01FBA00197A05 /* abc.jpg */; };
75E33AED1C9CF3C600CBD0B8 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 75E33AEC1C9CF3C600CBD0B8 /* main.m */; };
75E33AF01C9CF3C600CBD0B8 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 75E33AEF1C9CF3C600CBD0B8 /* AppDelegate.m */; };
75E33AF31C9CF3C600CBD0B8 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 75E33AF21C9CF3C600CBD0B8 /* ViewController.m */; };
Expand All @@ -24,6 +25,7 @@
/* Begin PBXFileReference section */
042B099277402A303BA96B6C /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
376022D6C7BDBC00C21502E8 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
75DE4ED91CA01FBA00197A05 /* abc.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = abc.jpg; sourceTree = "<group>"; };
75E33AE81C9CF3C600CBD0B8 /* T8NetworkKitDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = T8NetworkKitDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
75E33AEC1C9CF3C600CBD0B8 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
75E33AEE1C9CF3C600CBD0B8 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,6 +97,7 @@
75E33AF71C9CF3C600CBD0B8 /* Assets.xcassets */,
75E33AF91C9CF3C600CBD0B8 /* LaunchScreen.storyboard */,
75E33AFC1C9CF3C600CBD0B8 /* Info.plist */,
75DE4ED91CA01FBA00197A05 /* abc.jpg */,
75E33AEB1C9CF3C600CBD0B8 /* Supporting Files */,
);
path = T8NetworkKitDemo;
Expand Down Expand Up @@ -196,6 +199,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
75DE4EDA1CA01FBA00197A05 /* abc.jpg in Resources */,
75E33AFB1C9CF3C600CBD0B8 /* LaunchScreen.storyboard in Resources */,
75E33AF81C9CF3C600CBD0B8 /* Assets.xcassets in Resources */,
75E33AF61C9CF3C600CBD0B8 /* Main.storyboard in Resources */,
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions T8NetworkKitDemo/T8NetworkKitDemo/DemoService.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
+ (void)testRequestWithGetParmas:(GetParams *)getParams block:(RequestComplete)requestComplete;
+ (void)testRequestWithPostParams:(PostParams *)postParams block:(RequestComplete)requestComplete;
+ (void)testUploads:(T8FileModel *)fileModel block:(RequestComplete)requestComplete;
+ (void)testFilesUploads:(T8FileModelArray *)fileModelArray block:(RequestComplete)requestComplete;
@end
23 changes: 21 additions & 2 deletions T8NetworkKitDemo/T8NetworkKitDemo/DemoService.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,35 @@ + (void)testRequestWithPostParams:(PostParams *)postParams block:(RequestComplet
+ (void)testUploads:(T8FileModel *)fileModel block:(RequestComplete)requestComplete
{
NSString *urlPath = @"v2/upload/picture";
// NSString *urlPath = @"https://slack.com/api/files.upload?token=xoxp-28180523860-28139895139-28194787457-d910c3f484&filename=abc&pretty=1";

[T8NetworkBaseService uploadFile:fileModel urlPath:urlPath params:nil progressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {

} completBlock:^(RequestStatus status, NSDictionary *data, T8NetworkError *error) {
NSLog(@"bytesWritten:%lu", (unsigned long)bytesWritten);
NSLog(@"totalBytesWritten:%lld", totalBytesWritten);
} completeBlock:^(RequestStatus status, NSDictionary *data, T8NetworkError *error) {
if (requestComplete) {
requestComplete(status, data, error);
}
}];
}

+ (void)testFilesUploads:(T8FileModelArray *)fileModelArray block:(RequestComplete)requestComplete
{
NSString *urlPath = @"v2/upload/picture";
// NSString *urlPath = @"https://slack.com/api/files.upload?token=xoxp-28180523860-28139895139-28194787457-d910c3f484&filename=abc&pretty=1";

[T8NetworkBaseService uploadFiles:fileModelArray urlPath:urlPath params:nil progressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"bytesWritten:%lu", (unsigned long)bytesWritten);
NSLog(@"totalBytesWritten:%lld", totalBytesWritten);

} completeBlock:^(RequestStatus status, NSDictionary *data, T8NetworkError *error) {
if (requestComplete) {
requestComplete(status, data, error);
}
}];
}




@end
72 changes: 63 additions & 9 deletions T8NetworkKitDemo/T8NetworkKitDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ViewController.h"
#import "T8NetworkBaseService.h"
#import "AFNetworking.h"
#import "DemoService.h"
#import "PostParams.h"
#import "GetParams.h"
Expand All @@ -21,8 +22,9 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

[self testGet];
// Do any additional setup after loading the view, typically from a nib.
[self testFilesUpload];
// [self testUpload];

}

- (void)testPost
Expand Down Expand Up @@ -59,24 +61,76 @@ - (void)testUpload
[T8NetworkBaseService setBaseUrl:@"http://api-saas-dev.tinfinite.com"];
[T8NetworkBaseService setHeaderBlock:^(NSMutableURLRequest *request) {
[request setValue:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTZlNjdjYjc3ZTJjYzI4YTIxYjg5MzI3IiwiZGV2aWNlX2lkIjoiRTQ1RTIyQkMtQUYxMC00RTI2LUJDOUYtQUI5OEFFNzE4RDQ0IiwidHMiOjE0NTgyODgxMTc1MTMsImFwcF9pZCI6IjU2YzZjMzA5MjQzY2I3MjgyMDVhM2RmZiIsImlhdCI6MTQ1ODI4ODExN30.v4Sex80uTUVN7htZz-LoDuaqHLFmtPlzcZ5jxGHXUzI" forHTTPHeaderField:@"x-access-token"];
[request setValue:@"ewogICJwaG9uZV9tb2RlbCIgOiAiaVBob25lIiwKICAicmVsZWFzZV9jaGFubmVsIiA6ICJhcHBfc3RvcmUiLAogICJhcHBfdmVyc2lvbiIgOiAiMTAwIiwKICAib3NfdmVyc2lvbiIgOiAiOS4yLjEiLAogICJkZXZpY2VfdG9rZW4iIDogIjZjNGI2NGQ5ZDgzYTVlNjY0ZDg3N2EwNTRhODMzZWNiMzg4MmQyOWVlYTdmZWNkZTE2YWEzMGFkMzQ3MzEzYTgiLAogICJwbGF0Zm9ybSIgOiAiaU9TIgp9" forHTTPHeaderField:@"x-device-info"];
[request setValue:@"56c6c309243cb728205a3dff" forHTTPHeaderField:@"x-app-id"];
}];

UIImage *image = [UIImage imageNamed:@"abc"];
UIImage *image = [UIImage imageNamed:@"bcd"];
if (image) {
NSLog(@"image not nil");
}
NSData *data = UIImageJPEGRepresentation(image, 0.3);
NSData *data = UIImageJPEGRepresentation(image, 1);
T8FileModel *fileModel = [[T8FileModel alloc]init];
fileModel.data = data;
fileModel.type = FileModelPath;
fileModel.type = FileModelData;
fileModel.mimeType =@"image/jpg";
fileModel.path = @"/Users/Ryeagle/Downloads/bcd.jpg";

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"jpg"] ;
fileModel.path = filePath;
UIImage *image1 = [UIImage imageWithContentsOfFile:fileModel.path];
if (image1) {
NSLog(@"image1 is not nil");
}
fileModel.name = @"file";
fileModel.fileName = @"abc.jpg";

[DemoService testUploads:fileModel block:^(RequestStatus status, NSDictionary *data, T8NetworkError *error) {
}];
}

- (void)testFilesUpload
{
[T8NetworkBaseService setBaseUrl:@"http://api-saas-dev.tinfinite.com"];
[T8NetworkBaseService setHeaderBlock:^(NSMutableURLRequest *request) {
[request setValue:@"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiNTZlNjdjYjc3ZTJjYzI4YTIxYjg5MzI3IiwiZGV2aWNlX2lkIjoiRTQ1RTIyQkMtQUYxMC00RTI2LUJDOUYtQUI5OEFFNzE4RDQ0IiwidHMiOjE0NTgyODgxMTc1MTMsImFwcF9pZCI6IjU2YzZjMzA5MjQzY2I3MjgyMDVhM2RmZiIsImlhdCI6MTQ1ODI4ODExN30.v4Sex80uTUVN7htZz-LoDuaqHLFmtPlzcZ5jxGHXUzI" forHTTPHeaderField:@"x-access-token"];
}];

UIImage *image = [UIImage imageNamed:@"bcd"];
NSData *data = UIImageJPEGRepresentation(image, 0.3);
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"jpg"] ;
NSLog(@"%@", filePath);
T8FileModel *fileModel1 = [[T8FileModel alloc]init];
T8FileModel *fileModel2 = [[T8FileModel alloc]init];
T8FileModel *fileModel3 = [[T8FileModel alloc]init];


fileModel1.data = data;
fileModel1.type = FileModelData;
fileModel1.mimeType =@"image/jpg";
fileModel1.path = filePath;
fileModel1.name = @"file1d";
fileModel1.fileName = @"abc.jpg";

fileModel2.data = data;
fileModel2.type = FileModelData;
fileModel2.mimeType =@"image/jpg";
fileModel2.path = filePath;
fileModel2.name = @"filedd";
fileModel2.fileName = @"bcd.jpg";

fileModel3.data = data;
fileModel3.type = FileModelPath;
fileModel3.mimeType =@"image/jpg";
fileModel3.path = filePath;
fileModel3.name = @"filebb";
fileModel3.fileName = @"cde.jpg";


T8FileModelArray *fileModelArray = [[T8FileModelArray alloc]init];
fileModelArray.fileModelArray = @[fileModel1, fileModel2, fileModel3];

[DemoService testFilesUploads:fileModelArray block:^(RequestStatus status, NSDictionary *data, T8NetworkError *error) {

}];
}



@end
Binary file added T8NetworkKitDemo/T8NetworkKitDemo/abc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.