Upload image and data with iPhone sdk

first param is NSDictionary datatype.
and below
[Property getPropValue:@"Webservice"];
it is returning me like: http://myserver.com/upload
is getting URL, you can put your own url (server , which can get File data)

Below is code for upload

+(NSString *) uploadImageWithData:(NSMutableDictionary *)record image:(NSData *)imageData
	NSString *returnString;
	NSString *urlString = [Property getPropValue:@"Webservice"];
	NSURL *url = [NSURL URLWithString:urlString];
	NSString *filename = @"userfile";
	NSMutableURLRequest *request= [[NSMutableURLRequest alloc] initWithURL:url] ;
	[request setURL:[NSURL URLWithString:urlString]];
	[request setHTTPMethod:@"POST"];
	NSString *boundary = @"---------------------------14737809831466499882746641449";
	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
	[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
	NSMutableData *postbody = [NSMutableData data];
	//NSMutableDictionary * params = [[NSMutableDictionary alloc] initWithObjects:[[NSArray alloc] initWithObjects:@"great problem", @"infront of my house",nil] forKeys:[[NSArray alloc] initWithObjects:@"problem",@"location",nil]];
	NSString *postData = [Webservices getHTTPBodyParamsFromDictionary:record];
//here Webservices is my class name
	[postbody appendData:[postData dataUsingEncoding:NSUTF8StringEncoding]];
	if(imageData != nil)
		[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
		[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filetype=\"image/jpeg\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
		[postbody appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
		[postbody appendData:[NSData dataWithData:imageData]];
	[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
	[request setHTTPBody:postbody];
	NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
	returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
	[request release];
	return [returnString autorelease];

for post data, I had made getHTTPBodyParamsFormDictionary method,

which takes params as NSMutableDictionary, you can change it to NSDictionary if you want.

Next Page

7 thoughts on “Upload image and data with iPhone sdk

  1. when i am trying to post image data only then its working fine but while trying to post text along with image API throws an error. Can you suggest me some way to come out of this error.

  2. i want to upload image and send some data as dictionary format, i need some clear explanation about this getHTTPBodyParamsFormDictionary method.

  3. Pingback: How to post an image to the web services closed | Code and Programming

  4. I think all I need is a little clarification on the line of code,
    NSString *postData = [Webservices getHTTPBodyParamsFromDictionary:record];

    I am only trying to post an image and a single parameter, which is an xml string.

    Is Webservices the class and method call?

    My code looks something like this,

    - (BOOL)updatePackageRecordWithImage:(NSString *)image64String data:(NSString *) xmlData

    NSURLResponse *response = nil;
    NSError *error = nil;

    NSString *soapMessage = [NSString stringWithFormat:
    "\n",image64String, xmlData];

    NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.WMGSvc.jedis.com/service.asmx"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:locationOfWebService];
    NSString *msgLength = [NSString stringWithFormat:@”%lu”,(unsigned long) [soapMessage length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"http://www.WMGSvc.jedis.com/UpdatePackageRecordWithImage" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>