您可以一一附加参数,例如:-
NSURL *dataURL=[[NSURL alloc]initWithString:[NSString stringWithFormat:@"http://182.73.152.59:82/php/tauky_services/codeigniter-restserver-master/index.php/api/uploadClass/uploadTauky/"]];
NSMutableURLRequest *dataRqst = [NSMutableURLRequest requestWithURL:dataURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[dataRqst setHTTPMethod:@"POST"];
NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",stringBoundary];
[dataRqst addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// -------------------- ---- caption ---------------------------\\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"captionType\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[caption dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// - - - - - - - - - - - - 用户身份 - - - - - - - - - - - - ----\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"UserIdType\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[userId dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// -------------------- ---- mobile_tauky_id ------------------------ ----\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"mobile_tauky_idType\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[mobile_tauky_id dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// -------------------- ---- blauky_id ------------ ----\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"blauky_idType\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[blauky_id dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// media part
// -------------------- ---- Image Upload Status ---------------------------\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"type\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
NSString *mediaType=@"Image";
NSLog(@"type %@",mediaType);
[postBody appendData:[mediaType dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//pass MediaType file
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"Data\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: image/png\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// get the image data from main bundle directly into NSData object
NSData *imgData = UIImagePNGRepresentation(Your Image);
// add it to body
[postBody appendData:imgData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
// -------------------- ---- Audio Upload Status ---------------------------\
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"type\"\r\n\r\n"
dataUsingEncoding:NSUTF8StringEncoding]];
NSString *mediaType=@"Audio";
NSLog(@"type %@",mediaType);
[postBody appendData:[mediaType dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//pass MediaType file
[postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Disposition: form-data; name=\"Data\"; filename=\"myVoice.mp3\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: audio/caf\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *url = [NSString stringWithFormat:@"%@/record.mp3", documentsDirectory];
// get the audio data from main bundle directly into NSData object
NSData *audioData;
audioData = [[NSData alloc] initWithContentsOfFile:url];
// add it to body
[postBody appendData:audioData];
[postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
// final boundary
[postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
// add body to post
[dataRqst setHTTPBody:postBody];
NSHTTPURLResponse* response =[[NSHTTPURLResponse alloc] init];
NSError* error = [[NSError alloc] init] ;
//synchronous filling of data from HTTP POST response
NSData *responseData = [NSURLConnection sendSynchronousRequest:dataRqst returningResponse:&response error:&error];
//convert data into string
NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];
NSLog(@"Response String %@",responseString);