V
V
vladimir percov2016-11-16 14:21:28
Objective-C
vladimir percov, 2016-11-16 14:21:28

How to send 10 files?

Good day!
There is a code in php that takes data from the $_FILES['uploads'] array and uploads it to the server. In HTML with a form like this:

...
<input type="file" name="upload[]">
...

everything is loading fine.
How to implement dispatch in iOS in Objective-C?
I tried different codes from the Internet, nothing good happened.
If you had to send it like this in your projects, I would like a ready-made code, thanks)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
briahas, 2016-11-28
@briahas

It is not at all clear what exactly you tried, and what exactly did not work out for you, and what does PHP have to do with it.
The question is worded too broadly. Please correct it.

I
i_allar, 2016-12-03
@i_allar

AFNetworking library, my mime type is image, google what they are and put file type, name:@"avatar" - the field on the server in which you save the data
- (NSURLSessionDataTask *)saveAvatar:(UIImage *)image withCompletion:(CompletionWithErrorBlock )completion {
NSString *apiPath = [NSString stringWithFormat:@"%@/api/avatars", [self currentHost]];
AFHTTPSessionManager *manager = [self getManager];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSString *token = [NSString stringWithFormat:@"%@", ;
if(!token length)
{
completion([NSError errorWithDomain:@"saveAvatar:" code:-33002 userInfo:@{@"Error":@"token from NSUserDefaults is nil"}]);
return nil;
}
NSDictionary *dict = @{@"access_token":token
};
NSNumber *userId = [[NSUserDefaults standardUserDefaults] objectForKey:kAuthorizedUserId];
__block NSString *imageName = [NSString stringWithFormat:@"avatarForUserWithId:%@",userId];
__block typeof(UIImage) *imageInstance = image;
__weak typeof(self) selfInstance = self;
NSURLSessionDataTask *task = [manager POST:apiPath
parameters:dict
constructingBodyWithBlock:^(id _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"avatar" fileName:imageName mimeType:@"image/jpeg"]; // avarar - name of field in database on server
}
progress:^(NSProgress * _Nonnull uploadProgress)
{
}
success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
completion(nil);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
[selfInstance reloginWithCompletion:^(NSError *error) {
if (error) {
completion(error);
return;
}
[selfInstance saveAvatar:imageInstance withCompletion:completion];
}];
}];
return task;
}
- (AFHTTPSessionManager *)getManager {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"image/jpeg", @"image/gif", @"image/png", @"multipart/form -data", nil];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
NSString *token = [NSString stringWithFormat:@"Bearer %@", ;
[manager.requestSerializer setValue:token forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
return manager;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question