4

我正在使用此代码,但问题是它在 nsdata 转换块中对 Haxcode 进行编码,我想发送相同的代码,我使用固定整数的用户 ID 获得。请帮忙。

 NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image1, 0)];
    //image.image=image1;

    [Base64 initialize];
    NSString *b64EncStr = [Base64 encode:imageData];

    NSLog(@"encoded.%@",b64EncStr);




     NSURL *url = [[NSURL alloc] initWithString:updateimageURL];
    NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:url];
    [req setHTTPMethod:@"POST"];

    NSString *trimmed = [b64EncStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];



    paramDataString = [NSString stringWithFormat:@"Id=%d&FromString=%@",100,trimmed];

    NSLog(@"%@",paramDataString);


    NSData* aData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];


      [req setHTTPBody: aData];

    NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:req delegate:self];
    if (theConnection)
    {
        NSMutableData *data = [[NSMutableData alloc] init];
        self.receivedData=data;
        [data release];

    }
    else {

        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:nil message:@"Check your networking configuration." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    }

    [url release];
    [req release];

它以正确的方式建立 URL 连接,但响应是服务器错误。

4

2 回答 2

14

首先将 NSData 转换UImageNSDatabase64string,然后将其传递给您的 Web 服务

finalImagePath = [imageData base64EncodedString];

NSString *strImageData = [finalImagePath stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

然后发送strImageData到您的网络服务。

从服务器使用中检索时

NSString *strImageData = [finalImagePath stringByReplacingOccurrencesOfString:@"%2B" withString:@"+"];
于 2013-03-14T06:06:54.423 回答
2

将 base64 字符串直接上传到您的服务器

paramDataString = [NSString stringWithFormat:@"Id=%d&FromString=%@",100,b64EncStr];
于 2013-03-07T07:47:34.457 回答