问题描述
我试图将一个音频文件和一个JSON字符串发送到PHP服务器。用下面的代码将发送JSON,但文件到达空。如果我把JSON块放在音频块下面,文件完全到达,但是JSON是空的。 (NSString *)filePath URL:(NSString *)urlString JSON:(NSData *){/ * )jsonBody
{
NSString * jsonString = [[NSString alloc] initWithData:jsonBody encoding:NSUTF8StringEncoding];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@POST];
$ b $ if([defaults objectForKey:@UserToken]){
//将头添加到请求中。
[request addValue:[defaults objectForKey:@UserToken] forHTTPHeaderField:@token];
}
NSString * boundary = [NSString stringWithString:@--------------------------- 14737809831466499882746641449\" ];
NSString * contentType = [NSString stringWithFormat:@multipart / form-data; boundary =%@,boundary];
[request addValue:contentType forHTTPHeaderField:@Content-Type];
NSMutableData * body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Disposition:form-data; name = \body \\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Type:application / json\r\\\
\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@%@ \r\\\
,jsonString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Disposition:form-data; name = \recording\; filename = \recording.wav\\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@Content-Type:application / octet-stream\r\\\
\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:filePath]];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//设置请求体
[request setHTTPBody:body];
//发送请求(提交表单)并得到响应
NSData * returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@%@,returnString);
作为字符串参数,将二进制转换为Base64字符串。
看看的Base64编码信息。
帮助:)
I'm trying to send both an audio file and a JSON string to a PHP server. With the following code will send the JSON, but the file arrives empty. If I put the JSON block below the audio block, the file with completely arrive, but the JSON is empty. Any thoughts?
- (void)sendFile:(NSString *)filePath URL:(NSString *)urlString JSON:(NSData *)jsonBody
{
NSString *jsonString = [[NSString alloc] initWithData:jsonBody encoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
if ([defaults objectForKey:@"UserToken"]) {
// add the header to the request.
[request addValue:[defaults objectForKey:@"UserToken"] forHTTPHeaderField:@"token"];
}
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"body\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", jsonString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"recording\"; filename=\"recording.wav\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:filePath]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
// send the request (submit the form) and get the response
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"%@", returnString);
}
You can send the file easily as a string parameter by converting the binary to Base64 string.
Take a look at here for information on Base64 encoding on Mac and iOS.
Hope it helps :)
这篇关于在iOS中发送音频文件和JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!