I'm new to ios. How to send an image or an video to php server. i have a key "Image" for sending image and key "Video" to send video file. i need to send image or video with their keys, how can i send...?
Asked
Active
Viewed 425 times
2
-
1have a look on [AFNEtworking](https://github.com/AFNetworking/AFNetworking)!! – Ketan Parmar Dec 30 '16 at 06:50
-
yes but how to import AFNEtworking..? – Sasi Dec 30 '16 at 06:57
-
Possible duplicate of [Sending an HTTP POST request on iOS](http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios) – SOFe Dec 30 '16 at 07:15
1 Answers
2
Use Post
method. And you should archive image/video to data and make sheet data to send it to server.
I suggest you use Alamofire to do this. This is the code.
let imageData = UIPNGRepresentation(image)!
Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in
debugPrint(response)
}
Here is the link.
Update
As you're familiar with OC, use AFNetworking instead. This is the code.
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
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(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
Here is the link.

Lumialxk
- 6,239
- 6
- 24
- 47