Base64 encoded image corrupted during transfer to server

For some reason, I cannot upload the jpeg file to the server using the post method in NSURLRequest

I have an Android app that uses the same php code and can upload and download images by converting the image to an array of bytes, and then use base64 encoding to send and receive.

My iPhone app loads the image in order, the php script encodes using base64 , and I use the base64 decoder in my iPhone application, which then converts to an image. This works great.

However, image loading does not work.

I convert the image to base64 encoding (I used several different methods for base64 encoding , and all of them give the same result), after which I send a message to the server, decode it on the server side and save it to a file on the server.

The resulting decoded file on the server is a damaged jpeg image. The corrupt file size is 3 times larger than it should be.

The base64 encoded line generated for download is also very different from the base64 encoded line generated when the same jpeg file was downloaded from the server (i.e. the actual image file that I uploaded using ftp service).

My code is shown below along with a php script to get the image.

, - , base64 , .

base64 ?

NSString* comments = @"comments to go with image";


NSData *data = UIImageJPEGRepresentation(_imageView.image, 1);
NSString *base64EncodedImage = [NSString base64StringFromData: data length: data.length];



//load the team posts so we know what items have been posted and what haven't
NSMutableDictionary *postDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 comments, @"comment",
                                 base64EncodedImage , @"image",
                                 nil];


NSMutableArray *parts = [[NSMutableArray alloc] init];
for (NSString *key in postDict) {
    NSString *part = [NSString stringWithFormat: @"%@=%@", key, [postDict objectForKey:key]];
    [parts addObject:part];
}

NSString *encodedDictionary = [parts componentsJoinedByString:@"&"];

NSData *postData = [encodedDictionary dataUsingEncoding:NSUTF8StringEncoding];

NSString* url = @"http://www.scroppohunt.com/upload.php";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                   timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", postData.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    _data = [[NSMutableData data] init];

}     

php script,

<?php


$comments = $_REQUEST['comment'];


//do some database stuff with the comments

////////////////////////////////////////
//save the file to the images folder
///////////////////////////////////////////

$base=$_REQUEST['image'];

if(strlen($base) > 0)
{

    // base64 encoded utf-8 string

    $binary=base64_decode($base);

    $file = fopen("postImages/test.jpg", 'wb');

    fwrite($file, $binary);

    fclose($file);
}

//display the bae64 encoded string taht was uploaded
echo $base;

? >

+5
3

, - , , - (, ) , :

Base64 "+" "%". , :

NSString* encodedImageString = [base64EncodedImage stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

base64EncodedImage postDict encodedImageString.

, postDict :

NSMutableDictionary *postDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 comments, @"comment",
                                 encodedImageString , @"image",
                                 nil];

, , , .

.

+11

. , "+" "" . :

enter image description here

+2

Try changing the type of content.

[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
0
source

All Articles