我是iPhone编程新手。任何人都可以告诉我,我已经上传了图像和音频以及这些参数
输入参数:caption
,user_id
,mobile_tauky_id
,blauky_id
,image
,audio
(caption
和{{ 1}}是可选的)
使用以下代码,我可以上传blauky_id
,同样我想先上传image
,caption
表示一些整数值,例如:user_id
,3
一些整数值ex:mobile_tauky_id
,5
也是例如:blauky_id
,2
和image
。
我可以在哪里添加这些参数首先我要添加标题,然后是audio
,然后是user_id
,mobile_tauky_id
,之后我必须附加blauky_id
和{{1 }}。任何机构都可以告诉我如何在下面的代码中附加这些参数。我只能追加image
,但在此之前我想要附加标题,audio
,image
。
user_id
答案 0 :(得分:7)
您可以逐个追加参数,例如: -
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]];
// -------------------- ---- user_id -------------------- ------- \
[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);