Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)upload
- {
- // Coded : Sweeta ahuja..
- Conversation *objConve = [Conversation getConversationWithId:_objMessage.conversationId];
- // UIImage *uploadImage = [UIImage imageWithContentsOfFile:_objMessage.mediaPath];
- NSMutableDictionary *_params = [[NSMutableDictionary alloc] init];
- [_params setObject:@(ShareObj.loginUser.userId) forKey:@"senderId"];
- [_params setObject:@(objConve.userId) forKey:@"receiverId"];
- [_params setObject:@(0) forKey:@"groupId"];
- [_params setObject:@(_objMessage.msgType) forKey:@"mediaType"];
- // the boundary string : a random string, that will not repeat in post data, to separate post data fields.
- NSString *BoundaryConstant = @"----------V2ymHFg03ehbqgZCaKO6jy";
- // string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
- NSString* FileParamConstant = @"file";
- // The server url to which the image (or the media) is uploaded. Use your server url here...
- NSURL* requestURL = [NSURL URLWithString:[NSString stringWithFormat:kUploadMediaPath]];
- // create request
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
- [request setHTTPShouldHandleCookies:NO];
- [request setTimeoutInterval:30];
- [request setHTTPMethod:@"POST"];
- // set Content-Type in HTTP header
- NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", BoundaryConstant];
- [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
- // post body
- NSMutableData *body = [NSMutableData data];
- // add params (all params are strings)
- for (NSString *param in _params) {
- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"%@\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
- }
- // add image data
- NSData *mediaData = [NSData dataWithContentsOfFile:_objMessage.mediaPath];
- if (mediaData) {
- [body appendData:[[NSString stringWithFormat:@"--%@\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", FileParamConstant,_objMessage.mediaName] dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:mediaData];
- [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
- }
- [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
- // setting the body of the post to the reqeust
- [request setHTTPBody:body];
- // set the content-length
- NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]];
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- // set URL
- [request setURL:requestURL];
- //__block NSString *stringForText = @"";
- ///////////////////////////////////////////////////
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
- uploadTask = [manager
- uploadTaskWithStreamedRequest:request
- progress:^(NSProgress * _Nonnull uploadProgress) {
- // This is not called back on the main queue.
- // You are responsible for dispatching to the main queue for UI updates
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"Progress : %f",[uploadProgress fractionCompleted]);
- [self.circleChart setProgress:[uploadProgress fractionCompleted]*100];
- if([uploadProgress fractionCompleted] == 1)
- {
- [self complateUploading];
- }
- });
- }
- completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
- if (error)
- {
- NSLog(@"Error: %@", error);
- }
- else
- {
- NSLog(@"%@ %@", response, responseObject);
- }
- }];
- [uploadTask resume];
- }
Add Comment
Please, Sign In to add comment