Açıklama Yok

UploadFileModel.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // UploadFileModel.m
  3. // FirstLink
  4. //
  5. // Created by unicode on 14-10-11.
  6. // Copyright (c) 2014年 FirstLink. All rights reserved.
  7. //
  8. #import "UploadFileModel.h"
  9. #import "FLDataCenter.h"
  10. #import "User.h"
  11. @implementation UploadFileModel
  12. + (void)uploadFile:(NSData *)data
  13. fileType:(NSString *)type
  14. contentType:(NSString *)contenttype
  15. parameters:(NSDictionary *)parameters
  16. success:(void (^)(MSGHeader *, id))success
  17. progress:(void (^)(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))progress
  18. failure:(void (^)(MSGHeader *, NSError *))failure
  19. {
  20. if (!data || !contenttype || !type) {
  21. return;
  22. }
  23. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  24. [para setValue:contenttype forKey:@"contenttype"];
  25. [para setValue:type forKey:@"type"];
  26. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  27. if (parameters) {
  28. [para addEntriesFromDictionary:parameters];
  29. }
  30. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/upload.json?", [[FKServerUtil sharedInstance] apiServer]];
  31. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  32. [dataCenter POSTData:data
  33. name:@"imagefile"
  34. fileName:@"photo.jpeg"
  35. mimeType:@"image/jpeg"
  36. url:[NSURL URLWithString:URLString]
  37. parameters:para
  38. success:^(MSGHeader *header, id responseObject) {
  39. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  40. success(header, [UploadFileModel parserURL:responseObject]);
  41. } else {
  42. success(header, responseObject);
  43. }
  44. }
  45. progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
  46. if (progress) {
  47. progress(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  48. }
  49. } failure:^(MSGHeader *header, NSError *error) {
  50. failure(header, error);
  51. }];
  52. }
  53. + (void)uploadFileNotLogin:(NSData *)data
  54. fileType:(NSString *)type
  55. contentType:(NSString *)contenttype
  56. parameters:(NSDictionary *)parameters
  57. success:(void (^)(MSGHeader *, id))success
  58. failure:(void (^)(MSGHeader *, NSError *))failure
  59. {
  60. if (!data || !contenttype || !type) {
  61. return;
  62. }
  63. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  64. [para setValue:contenttype forKey:@"contenttype"];
  65. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  66. if (parameters) {
  67. [para addEntriesFromDictionary:parameters];
  68. }
  69. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/user/upload.json?", [[FKServerUtil sharedInstance] apiServer]];
  70. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  71. [dataCenter POSTData:data
  72. name:@"imagefile"
  73. fileName:@"photo.jpeg"
  74. mimeType:@"image/jpeg"
  75. url:[NSURL URLWithString:URLString]
  76. parameters:para
  77. success:^(MSGHeader *header, id responseObject) {
  78. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  79. success(header, [UploadFileModel parserURL:responseObject]);
  80. } else {
  81. success(header, responseObject);
  82. }
  83. } progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
  84. } failure:^(MSGHeader *header, NSError *error) {
  85. failure(header, error);
  86. }];
  87. }
  88. + (void)uploadLogFile:(NSData *)data
  89. fileType:(NSString *)type
  90. contentType:(NSString *)contenttype
  91. parameters:(NSDictionary *)parameters
  92. success:(void (^)(MSGHeader *, id))success
  93. failure:(void (^)(MSGHeader *, NSError *))failure {
  94. if (!data || !contenttype || !type) {
  95. return;
  96. }
  97. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  98. [para setValue:type forKey:@"type"];
  99. [para setValue:contenttype forKey:@"content_type"];
  100. if (parameters) {
  101. [para addEntriesFromDictionary:parameters];
  102. }
  103. [para addEntriesFromDictionary:[FLRequestHelper commonParamaterForLogin]];
  104. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/util/upload_file.json", [[FKServerUtil sharedInstance] apiServer]];
  105. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  106. [dataCenter POSTData:data
  107. name:@"file"
  108. fileName:@"filename"
  109. mimeType:contenttype
  110. url:[NSURL URLWithString:URLString]
  111. parameters:para
  112. success:^(MSGHeader *header, id responseObject) {
  113. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  114. success(header, [UploadFileModel parserURL:responseObject]);
  115. } else {
  116. success(header, responseObject);
  117. }
  118. } progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
  119. } failure:^(MSGHeader *header, NSError *error) {
  120. failure(header, error);
  121. }];
  122. }
  123. - (NSString*)parserURL:(NSDictionary*)jsonDict
  124. {
  125. return [[jsonDict objectForKey:@"data"] objectForKey:@"url"];
  126. }
  127. + (NSString*)parserURL:(NSDictionary*)jsonDict
  128. {
  129. return [[jsonDict objectForKey:@"data"] objectForKey:@"url"];
  130. }
  131. #pragma mark - Upload Address Book
  132. + (void)uploadAddressBook:(NSString *)string
  133. mobile:(NSString *)mobile
  134. totalNum:(NSString*)totalNum
  135. currNum:(NSString*)currNum
  136. success:(void (^)(MSGHeader *, id))success
  137. failure:(void (^)(MSGHeader *, NSError *))failure {
  138. if (!string) {
  139. return;
  140. }
  141. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  142. [para setValue:string forKey:@"bdata"];
  143. [para setValue:mobile forKey:@"mobile"];
  144. [para setValue:totalNum forKey:@"totalnum"];
  145. [para setValue:currNum forKey:@"currnum"];
  146. [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
  147. NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/create_address_book.json?", [[FKServerUtil sharedInstance] apiServer]];
  148. FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
  149. [dataCenter POST:URLString parameters:para success:^(MSGHeader *header, id responseObject) {
  150. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  151. success(header, [UploadFileModel parserURL:responseObject]);
  152. } else {
  153. success(header, responseObject);
  154. }
  155. } failure:^(MSGHeader *header, NSError *error) {
  156. failure(header, error);
  157. }];
  158. }
  159. @end