123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- //
- // UploadFileModel.m
- // FirstLink
- //
- // Created by unicode on 14-10-11.
- // Copyright (c) 2014年 FirstLink. All rights reserved.
- //
- #import "UploadFileModel.h"
- #import "FLDataCenter.h"
- #import "User.h"
- @implementation UploadFileModel
- + (void)uploadFile:(NSData *)data
- fileType:(NSString *)type
- contentType:(NSString *)contenttype
- parameters:(NSDictionary *)parameters
- success:(void (^)(MSGHeader *, id))success
- progress:(void (^)(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite))progress
- failure:(void (^)(MSGHeader *, NSError *))failure
- {
- if (!data || !contenttype || !type) {
- return;
- }
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:contenttype forKey:@"contenttype"];
- [para setValue:type forKey:@"type"];
- [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
- if (parameters) {
- [para addEntriesFromDictionary:parameters];
- }
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/upload.json?", [[FKServerUtil sharedInstance] apiServer]];
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
-
- [dataCenter POSTData:data
- name:@"imagefile"
- fileName:@"photo.jpeg"
- mimeType:@"image/jpeg"
- url:[NSURL URLWithString:URLString]
- parameters:para
- success:^(MSGHeader *header, id responseObject) {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [UploadFileModel parserURL:responseObject]);
- } else {
- success(header, responseObject);
- }
- }
- progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
- if (progress) {
- progress(bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (void)uploadFileNotLogin:(NSData *)data
- fileType:(NSString *)type
- contentType:(NSString *)contenttype
- parameters:(NSDictionary *)parameters
- success:(void (^)(MSGHeader *, id))success
- failure:(void (^)(MSGHeader *, NSError *))failure
- {
- if (!data || !contenttype || !type) {
- return;
- }
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:contenttype forKey:@"contenttype"];
- [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
- if (parameters) {
- [para addEntriesFromDictionary:parameters];
- }
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/user/upload.json?", [[FKServerUtil sharedInstance] apiServer]];
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
-
- [dataCenter POSTData:data
- name:@"imagefile"
- fileName:@"photo.jpeg"
- mimeType:@"image/jpeg"
- url:[NSURL URLWithString:URLString]
- parameters:para
- success:^(MSGHeader *header, id responseObject) {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [UploadFileModel parserURL:responseObject]);
- } else {
- success(header, responseObject);
- }
- } progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
-
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- + (void)uploadLogFile:(NSData *)data
- fileType:(NSString *)type
- contentType:(NSString *)contenttype
- parameters:(NSDictionary *)parameters
- success:(void (^)(MSGHeader *, id))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- if (!data || !contenttype || !type) {
- return;
- }
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:type forKey:@"type"];
- [para setValue:contenttype forKey:@"content_type"];
- if (parameters) {
- [para addEntriesFromDictionary:parameters];
- }
- [para addEntriesFromDictionary:[FLRequestHelper commonParamaterForLogin]];
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/util/upload_file.json", [[FKServerUtil sharedInstance] apiServer]];
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
-
- [dataCenter POSTData:data
- name:@"file"
- fileName:@"filename"
- mimeType:contenttype
- url:[NSURL URLWithString:URLString]
- parameters:para
- success:^(MSGHeader *header, id responseObject) {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [UploadFileModel parserURL:responseObject]);
- } else {
- success(header, responseObject);
- }
- } progress:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
-
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- - (NSString*)parserURL:(NSDictionary*)jsonDict
- {
-
- return [[jsonDict objectForKey:@"data"] objectForKey:@"url"];
- }
- + (NSString*)parserURL:(NSDictionary*)jsonDict
- {
-
- return [[jsonDict objectForKey:@"data"] objectForKey:@"url"];
- }
- #pragma mark - Upload Address Book
- + (void)uploadAddressBook:(NSString *)string
- mobile:(NSString *)mobile
- totalNum:(NSString*)totalNum
- currNum:(NSString*)currNum
- success:(void (^)(MSGHeader *, id))success
- failure:(void (^)(MSGHeader *, NSError *))failure {
- if (!string) {
- return;
- }
-
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- [para setValue:string forKey:@"bdata"];
- [para setValue:mobile forKey:@"mobile"];
- [para setValue:totalNum forKey:@"totalnum"];
- [para setValue:currNum forKey:@"currnum"];
- [para addEntriesFromDictionary:[FLRequestHelper commonParamater]];
-
- NSString *URLString = [NSString stringWithFormat:@"%@/link-site/api/create_address_book.json?", [[FKServerUtil sharedInstance] apiServer]];
-
- FLDataCenter *dataCenter = [FLDataCenter sharedDataCenter];
- [dataCenter POST:URLString parameters:para success:^(MSGHeader *header, id responseObject) {
- if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
- success(header, [UploadFileModel parserURL:responseObject]);
- } else {
- success(header, responseObject);
- }
- } failure:^(MSGHeader *header, NSError *error) {
- failure(header, error);
- }];
- }
- @end
|