在iPhone应用程序使用的Base64着上传图片 [英] cant upload picture using Base64 in iphone app

查看:187
本文介绍了在iPhone应用程序使用的Base64着上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个论坛之前问这个,但它被关闭我不知道为什么,所以我再次发布我的问题。
在iPhone应用程序,我有使用Base64编码上传图片,但是当我在服务器看起来画面全白(大小=为0x0,54 KO),我确定我的Base64 EN $我的照片的C $ C是正确的因为我有一个PHP脚本,我用它和画面显示正常。
这里是用来上传PHP code:

i ask this before in this forum but it was closed i dont know why, so i post my question again. in an iphone app i have to upload a picture using Base64 encoding, but when i look in the server the picture is all white ( size = 0x0 ,54 KO ), im sure that my Base64 encode of my pic is correct because i have a php script ,i use it and the picture appear normally. here is the php code used to upload :

<?php
$filename = "photo_to_upload.jpg";
$handle = fopen($filename, "r");
$imgbinary = fread($handle, filesize($filename));
$data = base64_encode($imgbinary);
fclose($handle);
?>
<img src="./<?php echo $filename ?>" />
<form action="http://host" method="POST">
<input type="hidden" name="data" value="<?php echo $data?>">
<input type="submit" value="envoyer" />
</form>

和X code我用这个:

and the Xcode i use this :

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"photo_to_upload" ofType:@"jpg"];
 NSURL *url = [NSURL fileURLWithPath:filePath]; 
 NSData *imageData = [NSData dataWithContentsOfURL:url];

 NSURLResponse* response;
 NSError* error=nil;

NSMutableData *postData=[[NSMutableData alloc]init];
[postData appendData:[@"data=" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[self base64forData:imageData]];


NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL   URLWithString:@"HOST"]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

[[NSURLConnection alloc] initWithRequest:request delegate:self];    

想你的任何帮助。

think you for any help.

推荐答案

我也面临着同样的问题红麦答案帮助了我。

I too face the same Issue Red Mak Answer Helped me.

答案,帮助

解决!其实,问题是,服务器连接code的要求,即它去code与布兰克取代+后+代替相思,但为Base64我们配发的+,第一步服务器什么也不做(不相思找到),但在它Seconde系列更换+,并且给出一个错误的base64 code,

SOLVED !!! in fact the problem was that server encode the request replacing the blanc with "+" after that it decode replacing the "+" with blanc ,but in Base64 we have allot of "+" ,the first step server do nothing( no blanc find) but in the seconde it replace the "+" and that give a wrong base64 code ,

to solve that i replac "+" with ASCII equivalente like this 

:[[self base64forData:imageData]stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"].

这篇关于在iPhone应用程序使用的Base64着上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆