问题
I have this code here that send a data to my server, this code works very well:
NSString *imagepath = [[self applicationDocumentsDirectory].path stringByAppendingPathComponent:@"ProgrammingWithObjectiveC.pdf"];
NSURL *outputFileURL = [NSURL fileURLWithPath:imagepath];
NSURL *icyURL = [NSURL URLWithString:uploadURLString];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:icyURL];
[request setHTTPMethod:@"POST"];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.app.upload"];
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
self.uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:outputFileURL];
[self.uploadTask resume];
As I say before, This code works very well, and the php file is:
<?php
$fp = fopen("myImage.pdf", "a");
$escreve = fwrite($fp, file_get_contents("php://input"));
fclose($fp);
?>
The problem is that I need to send some parameters in POST method like filename, id, and others, I try many things to do this, but I could not anything.
Other Solution
Well I was thinking how to solve this problem, I can send data but without send parameters, I can send parameters but without send data, but I can't send these two in the same request.
In my case I would need to send one image and a name of this image to my server, so the first think is send a image to my server with that code showing up!
In php I get this file, and generate a ramdom and long name for him, and return this name to Objective-c.
When Objective-c receive this response he send another request with the name of the file and the name of the file that we receive from the server. And now PHP receive this request and change the name of the file with the right name.
来源:https://stackoverflow.com/questions/29577741/send-data-and-parameter-to-server