0

您好,我是 iOS 新手,需要向 php 服务器提交图像。为此,我已将图像转换为 base64 格式。并且需要将此 base64 字符串发送到 PHP 服务器。我正在使用的代码是,请帮助我在 ios 中是全新的

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"comment=%@",@"test data"];
  // Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.sdevices.ru/flashrecorder/speechtotext.php"]];
// set Request Type
[request setHTTPMethod:@"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody:myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response); // here you get reasponse string

if ([response isEqualToString:@"Speech text!"]) {
    UIAlertView *alrt = [[UIAlertView alloc] initWithTitle:@"Flash Recorder" message:@"Text is submitted!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alrt show];
}
4

1 回答 1

1

我这篇文章有base 64 encode a data的答案

NSString 的 Base64 编码

对于字符串中的 NSData

NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];

和来自数据的 NSString

NSString* str =[NSString stringWithUTF8String:[data bytes]];

直接来自我的代码的这个片段确实很容易发布连接

-(BOOL) doPostConnection:(ServiceProps*) props delegate:(id<URLConnectionDelegate>) delegate
{
     NSString *post = props.contentString;
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
     NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:props.connectionURL];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Data-Type"];
    [request setHTTPBody:postData];
     NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:delegate];
     return conn != nil;
}

props -> 用于包装内容字符串和 url 的基本类

@interface ServiceProps : NSObject
@property () NSString* contentString;
@property () NSURL* connectionURL;
@end

委托 -> 一个包含您的后期服务逻辑的接口类,它取决于您的服务

@protocol URLConnectionDelegate <NSObject>

- (id) initWithConnectionDelegate:(id<ConnectionDelegate>)delegate;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
@end
于 2013-10-31T10:25:10.100 回答