Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to post text and image data in NSMutableUrlRequest?
    text
    copied!<p>I m supposed to post the session ID, that is generated during validation of credentials and image data. But the image and session ID is not uploaded. It was working fine when I used ASIHTTPRequest, later when i tried with NSMutableUrlRequest and NSURLConnection it doesnot seem to work.</p> <p>Given below is the code used to POST image and Session ID</p> <pre><code>- (void)postinDataToServer:(NSData *)inData { //inData is the image data. [inData retain]; NSString *urlString = @"http://xyz.com/abcd/kgh.php"; NSURL* url = [NSURL URLWithString:urlString]; NSMutableData *photoData = [[NSMutableData alloc]init]; //SessionID is stored in NSUserDefaults NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString * boundary = @"photoBoundaryParm"; NSString * boundaryString = [NSString stringWithFormat:@"--%@\r\n", boundary]; NSString * boundaryStringFinal = [NSString stringWithFormat:@"--%@--\r\n", boundary]; [photoData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"SessionID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[[NSString stringWithFormat:@"SessionID=%@",[defaults objectForKey:@"SessionID"]] dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\";\r\nfilename=\"myphoto.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:inData]; [photoData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [photoData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:photoData]; NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [photoData length]]; [request addValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"]; [request addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:NO]; [connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; [connection start]; [inData release]; inData = nil; } </code></pre> <p>Please help.</p> <p>I m stuck with this for really long time.</p> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload