lundi 27 avril 2015

Upload an image after 64-bit encoding


I want to upload an image on a server. I encode to 64-bit then I post. I am using this code:

NSURL *url=[[NSURL alloc] initWithString:@"http://ift.tt/1EBLi9u"];

NSMutableURLRequest *req=[[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:@"POST"];
// [req setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];

NSMutableData *postBody=[NSMutableData data];
NSString *boundary=@"*****";


[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"test.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *this=[pickedImageData base64EncodedString];

[postBody appendData: [this dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[req setHTTPBody:postBody];


NSHTTPURLResponse *response=nil;
        NSError *error=[[NSError alloc] init];

NSData *responseData;
    responseData=[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    NSLog(@"%@",responseData);
    if(responseData&&[responseData length])
    {

        NSDictionary *dictionary=[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
        NSLog(@"%@",dictionary);
    }
    else
    {
        NSLog(@"hello");
    }


Aucun commentaire:

Enregistrer un commentaire