Red*_*Mak 2 iphone upload base64
我之前在这个论坛上问这个,但它已经关闭我不知道为什么,所以我再次发布我的问题.在iPhone应用程序中我必须使用Base64编码上传图片,但当我在服务器中查看图片全白(大小= 0x0,54 KO),我确定我的图片的Base64编码是正确的,因为我有一个PHP脚本,我使用它,图片正常显示.这是用于上传的PHP代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
和Xcode我用这个:
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];
Run Code Online (Sandbox Code Playgroud)
认为你有任何帮助.
我也面对同样的问题Red Mak Answer帮助了我.
答案帮助
解决了 !!!实际上问题是服务器编码请求用"+"替换blanc,之后解码用blanc替换"+",但在Base64中我们分配了"+",第一步服务器什么都不做(没有blanc查找)但是在seconde中它替换了"+"并且给出了错误的base64代码,
to solve that i replac "+" with ASCII equivalente like this
:[[self base64forData:imageData]stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"].
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2492 次 |
| 最近记录: |