1

I want to upload an image in png format to a server.I have to give three parameters with the request which are

  • transactionID: (any random integer)

  • signatureBytes:data:image/png;base64,iVBORw0KGgoAAAsdffsdfsdfsdfsdfYAAAAeGRPoAAAgAElEQVR4nO2de5BmRXnGHzWmYkKVKUqJRk0erwerrwds55/lVnT/mm+87/Z4+3f30+/YN6D9L48CMA9VxnOJ9S

  • guid: CAD1CBE7-B5A4-43d6-BCDD-89B16F97E3C4

I have used the code:

NSData *imageData = UIImageJPEGRepresentation(image.image,90);

// setting up the URL to post to


NSString *urlString=@"http://www.riverport.net/icalibur/dev/signature.ashx";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];


NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"transactionID\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[[NSString stringWithString:@"7" intva] dataUsingEncoding:NSUTF8StringEncoding]];


  [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"signatureBytes\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"data:image/png;base64,iVBUaBCrFDXGSIECEXEFdUFEhEVAluXmsrrtrrttrrqddcsvrgergergg/YN6D9L4uozc5h+nsWsFhGSn88CWAAwqV2ynoz1qvJjYTmfCOk1QxJ0ANgHijoZDy8CMA9VxnOJ9S"] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"guid\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"CHJ78G-B5A4-43d6-BCDD-CFG567FGJ"] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ipodfile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];


NSString* returns= [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];



//NSLog(@"body %@",stri);


// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);

and the response from server is:

[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +201
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +66

[InvalidCastException: Conversion from string "����

Have anybody any idea What could be the problem.I do not know if i am doing it right .

Pankaj Kainthla
  • 2,439
  • 5
  • 31
  • 61
  • Duplicate of http://stackoverflow.com/questions/5801049/uploading-photo-through-programing-in-objective-c/5801303#5801303 and http://stackoverflow.com/questions/1939335/upload-image-from-iphone-to-the-server-folder – Chetan Bhalara May 09 '11 at 14:44

4 Answers4

3

From the errors you received i believe that you don't set your parameters correctly for the request.

Why don't you use ASIHTTPRequest to upload your photo? it has a class ASIFormDataRequest that you can use to make POST or GET requests and you can add parameters to it really easy.

here is an example of uploading images with ASIHTTPRequest.

Sorin Antohi
  • 6,145
  • 9
  • 45
  • 71
2

You're not formatting the upload data correctly. The post data should look something like this:

--boundary
Content-Disposition: form-data; name="foo"

value-for-foo
--boundary
Content-Disposition: form-data; name="bar"

value-for-bar
--boundary
Content-Disposition: form-data; name="file"; filename="filename.ext"
Content-Type: application/whatever

file data
--boundary--

Note how there is a blank line ("\r\n\r\n") in between the headers and the value of the field, a crlf after the value, and a repeat of the boundary before the next field's header. You have none of that in your code, your output looks more like this:

--boundary
Content-Disposition: form-data; name="foo"
value-for-fooContent-Disposition: form-data; name="bar"
value-for-barContent-Disposition: form-data; name="file"; filename="filename.ext"
Content-Type: application/whatever

file data--boundary--
Anomie
  • 92,546
  • 13
  • 126
  • 145
0

Hi Pankaj I am not sure but just try only image NSData with out transactionID and guide parameter, your code seems ok. Please let me know result.

Pankaj Gadhiya
  • 199
  • 2
  • 8
0

You say you want to post a PNG-image but you are fetching the JPEG-representation in your code-snippet. Is it solved by changing to UIImagePNGRepresentation?

iceydee
  • 1,009
  • 8
  • 10