0

I want to send an image to a web server running .Net which is not Restful .

I have tried a lot of things to convert the image into string and then sending it.
Like using initWithData:encoding: and also tried it with converting the image data into base64encodedstring by the method given in this question

iPhone to MS SQL Image Data Type Conversion Question

But no luck.

Then I thought of looking on facebook api for uploading images on facebook wall.
Using this tutorial the image can be uploaded to facebook

http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from-your-iphone-app

But the problem is, it is using asihttprequest to send the data on the facebook wall and I cannot see the raw data in the request will uploading the image . Can anyone help me out.

Community
  • 1
  • 1
Robin
  • 10,011
  • 5
  • 49
  • 75
  • what do you mean by raw data? the delegate methods of ASIHTTPRequest give you some insight on what's going on. – Faizan S. Sep 12 '11 at 09:17
  • The delegate methods only tells us that it is adding a file path to an object of type array and then doing asynchronous call to send the data to the webserver but it did not tells us how exactly is it sending the data, like in which format or what type of data is sent on the server. – Robin Sep 12 '11 at 09:41
  • 1
    In ASIHTTPRequestConfig.h, set DEBUG_FORM_DATA_REQUEST to 1 and it will log debug data to the console. – bbarnhart Jan 14 '12 at 03:48

1 Answers1

2

I tried it at my end. First I converted to base64encoding like this:

NSString *str64;

        if(Image){
            NSData *imageData = UIImageJPEGRepresentation(Image,0.75);
            str64 = [imageData base64Encoding];
        } 

Then I added into a dictionary and then added that dictionary into an array so that I can pass it as a JSON string in request's body.

NSMutableArray *ParaArray =[[NSMutableArray alloc]init];

    NSDictionary *ParaDictionary=[NSDictionary dictionaryWithObjectsAndKeys:
                                  [NSString stringWithFormat:@"%@",Userid], @"userid",
                                  [NSString stringWithFormat:@"%@",[Imagename URLEncodedString]], @"imagename",
                                  [NSString stringWithFormat:@"%@",DateNTime], @"datetime", 
                                  [NSString stringWithFormat:@"%@",ImageLocation], @"imagelocation",
                                  str64,@"image",
                                  nil]; // set the parameter


    [ParaArray addObject:ParaDictionary];

There after using ASIFormDataRequest I sent the image to server using this snippet:

ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlString]];

    [request setPostValue:[ParaArray JSONRepresentation]  forKey:@"bulk_data"];

    //[ParaDictionary release];
    [ParaArray release];

    [request setTimeOutSeconds:20];


    [request startSynchronous];

It worked fine for me. I hope it helps you as well.

Data is passed in form of a string and we can directly use the base64Encoding methods by copy pasting them. Though you might know them still here they are:

- (NSString *) base64Encoding {
return [self base64EncodingWithLineLength:0];
}

- (NSString *) base64EncodingWithLineLength:(NSUInteger) lineLength {
const unsigned char *bytes = [self bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
unsigned long ixtext = 0;
unsigned long lentext = [self length];
long ctremaining = 0;
unsigned char inbuf[3], outbuf[4];
unsigned short i = 0;
unsigned short charsonline = 0, ctcopy = 0;
unsigned long ix = 0;

while( YES ) {
    ctremaining = lentext - ixtext;
    if( ctremaining <= 0 ) break;

    for( i = 0; i < 3; i++ ) {
        ix = ixtext + i;
        if( ix < lentext ) inbuf[i] = bytes[ix];
        else inbuf [i] = 0;
    }

    outbuf [0] = (inbuf [0] & 0xFC) >> 2;
    outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
    outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
    outbuf [3] = inbuf [2] & 0x3F;
    ctcopy = 4;

    switch( ctremaining ) {
        case 1:
            ctcopy = 2;
            break;
        case 2:
            ctcopy = 3;
            break;
    }

    for( i = 0; i < ctcopy; i++ )
        [result appendFormat:@"%c", encodingTable[outbuf[i]]];

    for( i = ctcopy; i < 4; i++ )
        [result appendString:@"="];

    ixtext += 3;
    charsonline += 4;

    if( lineLength > 0 ) {
        if( charsonline >= lineLength ) {
            charsonline = 0;
            [result appendString:@"\n"];
        }
    }
}

return [NSString stringWithString:result];
}
Dip Dhingani
  • 2,499
  • 2
  • 20
  • 18
  • I am not asking for the code to transfer the image to a webserver, my question is what exact data is been sent over through the internet to the server, and also can us paste the method called base64Encoding. – Robin Sep 12 '11 at 09:43
  • data is sent over the internet in form of a string and we can copy paste the following methods for base64Encoding - – Dip Dhingani Sep 12 '11 at 11:04
  • Yes, absolutely, this is all. For further operations you've to handle it on the web end. I am happy I was of some help. – Dip Dhingani Sep 13 '11 at 05:31