I have this code below which sends an image and some text to my server:
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: nil];
NSString *requestURL = @"http://www.website.com.br/receive.php?name=StackOverflow";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestURL]];
[request setHTTPMethod:@"POST"];
UIImage *imagem = [UIImage imageNamed:@"Image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imagem, 1.0);
self.uploadTask = [self.session uploadTaskWithRequest:request fromData:imageData];
[self.uploadTask resume];
-(void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",newStr);
}
PHP
<?php
echo $_POST['name'];
?>
The problem with this code is that the didReceiveData
method does not receive the data going to the server, it only gets an NSData
when I put this code in php file:
print_r($_FILES);
And yet it returns an empty array, why this is happening?
Solved
Well, I solved my problem, lets go, In .h file you need to implement this protocols and one property:
< NSURLSessionDelegate, NSURLSessionTaskDelegate>
@property (nonatomic) NSURLSessionUploadTask *uploadTask;
whereas in .m file there is a method of IBAction type and that it is connected to a particular button existing in our view, we need only do this:
- (IBAction)start:(id)sender {
if (self.uploadTask) {
NSLog(@"Wait for this process finish!");
return;
}
NSString *imagepath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"myImage.jpg"];
NSURL *outputFileURL = [NSURL fileURLWithPath:imagepath];
// Define the Paths
NSURL *icyURL = [NSURL URLWithString:@"http://www.website.com/upload.php"];
// Create the Request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:icyURL];
[request setHTTPMethod:@"POST"];
// Configure the NSURL Session
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.sometihng.upload"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
// Define the Upload task
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:outputFileURL];
// Run it!
[self.uploadTask resume];
}
And implement some delegates methods:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
NSLog(@"didSendBodyData: %lld, totalBytesSent: %lld, totalBytesExpectedToSend: %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
if (error == nil) {
NSLog(@"Task: %@ upload complete", task);
} else {
NSLog(@"Task: %@ upload with error: %@", task, [error localizedDescription]);
}
}
And for finish, you need create a PHP file with this code:
<?php
$fp = fopen("myImage.jpg", "a");//If image come is .png put myImage.png, is the file come is .mp4 put myImage.mp4, if .pdf myImage.pdf, if .json myImage.json ...
$run = fwrite($fp, file_get_contents("php://input"));
fclose($fp);
?>