Pause/Resume file uploading to server from iphone - iphone

I am uploading video to server with HTTP posting.
How can I pause and resume this upload process by some button click
I am using the following code
NSString *urlString =#"http://sampleurl.com/upload_video";
NSMutableURLRequest *request= [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"user_id\"\r\n\r\n%#", appDelegate.userid] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"video\"; filename=\"%#\"\r\n", #"a.mov"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:file]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}

There is a connection delegate method which you can use
(void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite (NSInteger)totalBytesExpectedToWrite;
You can store the total bytes sent when you PAUSE the connection and then send the rest of data bytes to the web service. But remember, your web service should also be capable of collecting the data and then creating an image from it.
Hope I helped.

Related

How to change sendSynchronousRequest to sendASynchronousRequest in ios?

I am uploading image on server.
how to change Synchronous request to ASynchronous request. because i wanna use progressbar while uploading image..
My code is.
+(NSDictionary *)UploadPhoto :(UIImage *)image{
NSString *DeviceId;
NSString *TokenValue=tokenTemp;
DeviceId = DevID;
NSString *Nonce=[self generateRandomString];
NSString *currentTimeStamp = [self GetTimeStamp];
NSString *Cipher=[NSString stringWithFormat:#"%#%#%#%#%#",mySecret,DeviceId,Nonce,currentTimeStamp,TokenValue];
Cipher=[Cipher URLDecodedString];
Cipher=[self GetCipher:Cipher];
UIImage *TempPicture=image;//[UIImage imageNamed:imageName];
NSData *PictureData=UIImageJPEGRepresentation(TempPicture, 1.0);
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"%#Messages.php",WebServiceUrl]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setTimeoutInterval:20.0f];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"multipart/form-data; boundary=*****" forHTTPHeaderField:#"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
NSString *stringBoundary = [NSString stringWithString:#"*****"];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"%#",#"Upload"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"Type\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"%#",#"0"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"Name\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"%#",#"XYZ ****************"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"MessageImage\"; filename=\"c.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[postBody appendData:[NSData dataWithData:PictureData]];
[postBody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
[request setHTTPBody: postBody];
[request setValue:Cipher forHTTPHeaderField:#"Cipher"];
[request setValue:DeviceId forHTTPHeaderField:#"Deviceid"];
[request setValue:Nonce forHTTPHeaderField:#"Nonce"];
[request setValue:currentTimeStamp forHTTPHeaderField:#"Timestamp"];
[request setValue:TokenValue forHTTPHeaderField:#"Token"];
[request setHTTPBody:postBody];
NSError *error;
NSHTTPURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if (!data) {
NSLog(#"nil");
}
NSDictionary *temp= [response allHeaderFields];
NSString *temp2= [temp objectForKey:#"Responsecode"];
if(temp2!=nil && [temp2 length]>0)
{
}
else
{
}
return temp;
}
Thanks..
please help..
how to call
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite in this code..
If you want to want to track the upload progress, you have to use
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]
and implement all the NSURLConnectionDelegate methods. This is described in this Apple documentation.
In particular the connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: delegate methods informs you about the upload progress.
Another advantage of this method is that you can cancel a request, which is not possible with sendSynchronousRequest or sendASynchronousRequest.

NSURLConnection and NSURLConnectionDelegate work on different thread

as descripted in the title, I did some NSURLConnection like this. But I found that the delegate method couldn't get run. Anyone knows how to deal with it ?
edit:
my delegate works on the main thread, while NSURLConnection works on an operation queue. Now the case is :NSURLConnection works fine, but delegate won't run.
edit 2:
I used the class method [NSURLConnection connectionWithRequest: delegate:]
here is on my main queue
NSOperationQueue *queuePhoto = [[NSOperationQueue alloc] init];
NSInvocationOperation *invocationOperationPhotos = [[NSInvocationOperation alloc] initWithTarget:self selector:#selector(transferToServerAddimages:) object:arrayOfASection];
// [invocationOperationPhotos addObserver:self forKeyPath:#"isExecuting" options:0 context:invocationOperationPhotos];
// [invocationOperationPhotos addObserver:self forKeyPath:#"isCancelled" options:0 context:invocationOperationPhotos];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(<#selector#>) name:#"isExecuting" object:nil];
[invocationOperationPhotos setQueuePriority:NSOperationQueuePriorityHigh];
[queuePhoto addOperation:invocationOperationPhotos];
[mutableArrayPhotoQueue addObject:queuePhoto];
[invocationOperationPhotos release];
[queuePhoto release];
here is my NSURLConnnection:
- (void) transferToServerAddimages:(NSArray *) arrayToAdd
{
NSLog(#"[NSOperationQueue currentQueue]: %#", [NSOperationQueue currentQueue]);
NSString *murphoAppPrefix = [[AppDelegate sharedAppDelegate] murphoAppPrefix];
// setting up the request object now
NSString *urlString = [murphoAppPrefix stringByAppendingString:#"addPhotos.php"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
*/
// set header value , some random text that will never occur in the body
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
// email part
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"email\"\r\n\r\n%#", self.trip.whoCreate.email] dataUsingEncoding:NSUTF8StringEncoding]];
// password part
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"password\"\r\n\r\n%#", self.trip.whoCreate.psw] dataUsingEncoding:NSUTF8StringEncoding]];
// image part
NSInteger subCount=0;
for (NSDictionary *aDict in arrayToAdd) {
// belonging part
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"r_whichShortTrip%d\"\r\n\r\n%#", subCount, [aDict objectForKey:#"r_whichShortTrip"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uniqueId%d\"\r\n\r\n%#", subCount, [aDict objectForKey:#"uniqueId"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"createdTime%d\"\r\n\r\n%#", subCount, [aDict objectForKey:#"createdTime"]] dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *imageFile = [aDict objectForKey:#"image"];
NSData *imageData = UIImageJPEGRepresentation(imageFile, 1.0);
NSString *imageName = [[URLConnect createUUID] stringByAppendingString:#".jpg"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:
#"Content-Disposition: form-data; name=\"image%d\"; filename=%#\r\n", subCount, imageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *thumbnail = [aDict objectForKey:#"thumbnail"];
NSData *thumbnailData = [NSData dataWithData:UIImageJPEGRepresentation(thumbnail, 1)];
NSString *thumbnailName = [[URLConnect createUUID] stringByAppendingString:#".jpg"];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:
#"Content-Disposition: form-data; name=\"thumbnail%d\"; filename=%#\r\n", subCount++, thumbnailName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:thumbnailData]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// now lets make the connection to the web
[NSURLConnection connectionWithRequest:request delegate:self];
}
here is my delegate:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[[[AppDelegate sharedAppDelegate] mArrayOfFailedConnection] addObject:connection];
[connection cancel];
connection = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(#"response: %#", response);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(#"connection: %#", connection);
}
Actually when you set selector for operation as "transferToServerAddimages" that means when this function would executed completely your operation would be finished, also since NSUrlConnection calls its delegates on that same thread, on which it was started, so its probable that thread (OperationQueue) was finished before response arrived. If you indeed want to use NSOperationQueue then a better function to use is "sendAsynchronousRequest:queue:completionHandler:" of NSUrlConnection.
What's the delegate, the NSOperation? One possibility is your operation is calling -[connectionWithRequest:delegate:] and exit, which causes it to finish and be deleted. Put an NSLog in the operation's dealloc to see if its running.
Assuming all the operation does is NSURLConnection transations, you don't need to wrap the transaction in an NSOperation to get concurrency, since those transations are asynchronous already. But if you really want to use NSOperation (for coordination purposes etc), make it a "concurrent" one and put it on the main queue. Those kind are trickier but there are examples around.

how to upload image dynamically on server?

In my application i want to upload the images to server.
I am able to upload images on server but the name of the image mention in front of filename=%#.jpg. remains the same for images
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
but i want to save the image on server with the same image name it has......
plz help me out.
following is my code:
-(IBAction)upload:(id)sender
{
//imageview is a UIImageView
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 100);
// setting up the URL to post to
NSString *urlString = #"http://url.com/upload/uploader.php?";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\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];
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
///self.returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#",returnString);
}
change
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
to
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
where filename is a NSString* variable with your desired filename
this line
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
Content-Type: image/jpeg
instead of Content-Disposition: form-data
and learn how to use content type

NSData and Uploading Images via POST in iOS

I have been combing through the many many posts about uploading images via POST in iOS. Despite the wealth of information on this topic, I cannot manage to correctly upload JPEG data taken from my iPhone Simulator Photo Library.
The data, once on the server, is just a huge string of hexidecimal. Shouldn't NSData just be a byte stream? I don't get whats going on with all the hex, or why this code seems to work for everyone else.
Here is the code in question:
-(void)uploadWithUserLocationString:(NSString*)userLocation{
NSString *urlString = #"http://some.url.com/post";
// set up the form keys and values (revise using 1 NSDictionary at some point - neater than 2 arrays)
NSArray *keys = [[NSArray alloc] initWithObjects:#"auth",#"text",#"location",nil];
NSArray *vals = [[NSArray alloc] initWithObjects:self.authToken,self.textBox.text,userLocation,nil];
// set up the request object
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
//Add content-type to Header. Need to use a string boundary for data uploading.
NSString *boundary = [NSString stringWithString:#"0xKhTmLbOuNdArY"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
//create the post body
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
//add (key,value) pairs (no idea why all the \r's and \n's are necessary ... but everyone seems to have them)
for (int i=0; i<[keys count]; i++) {
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n",[keys objectAtIndex:i]] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",[vals objectAtIndex:i]] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
}
[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"image\"\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
[body appendData:[NSData dataWithData:self.imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
// set the body of the post to the reqeust
[request setHTTPBody:body];
// 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);
[keys release];
[vals release];
}
Thanks for your time.
This code works in my app. If you're not using ARC you'll need to modify the code to release anything alloc'ed.
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set URL
[request setURL:requestURL];
i hope this code will help some body else ...
//create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
//Set Params
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
//Create boundary, it can be anything
NSString *boundary = #"------VohpleBoundary4QuqLuM1cE5lMwCy";
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
//Populate a dictionary with all the regular values you would like to send.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:param1 forKey:#"param1-name"];
[parameters setValue:param2 forKey:#"param2-name"];
[parameters setValue:param3 forKey:#"param3-name"];
// add params (all params are strings)
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSString *FileParamConstant = #"imageParamName";
NSData *imageData = UIImageJPEGRepresentation(imageObject, 1);
//Assuming data is not nil we add this to the multipart form
if (imageData)
{
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type:image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
//Close off the request with the boundary
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// set URL
[request setURL:[NSURL URLWithString:baseUrl]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if ([httpResponse statusCode] == 200) {
NSLog(#"success");
}
}];
Take a look at Apple's SimpleURLConnections example project. You can use the PostController from there with a few modifications.
However - it's not exactly simple. The difference to the solution above is that Apple's is using a stream to stream the file to the server. That's much more memory-friendly than keeping the encoded image data around for the duration of the upload. It's also way more complicated.
NSData *imgData = UIImagePNGRepresentation(imgUser.image);
NSString *str=[NSString stringWithFormat:#"%#upload_image",appDelegate.strRoot];
NSString *urlString = [NSString stringWithFormat:#"%#",str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imgData]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter UserId
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[appDelegate.strUserID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
NSLog(#"%#",dict);
I see that you send multiple files in one request, correct?
From HTTP specification, you should use multipart/mixed Content-type inside embedding multipart/form-data
Example from link above:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y
--BbC04y
Content-disposition: attachment; filename="file1.txt"

Add Post data to Image Upload

I am posting an image from my IPhone app to a php page on a website using the code in this answer to this question. Essentially the code creates the correctly formatted html stream and submits it.
For Completeness, I have reposted the code.
- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename{
NSString *urlString = #"http://www.yourdomainName.com/yourPHPPage.php";
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:[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n",filename]] 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]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
return ([returnString isEqualToString:#"OK"]);
}
However I'm not clever enough :( to figure out how to add a general purpose field value eg. I would like to add a Comment field to be submitted along with the image file.
Any suggestions on how I could modify that code to add a number of fields to the POST form.
Thank you.
Update: I can get it to work by modifying the URL I post to to include GET Parameters, but I would rather use all post params.
It's not too difficult. The general format of a multipart/form-data post is like this:
--boundary-string
Content-Disposition: form-data; name="field1"
value1
--boundary-string
Content-Disposition: form-data; name="field2"
value2
--boundary-string--
File uploads are a special case, but you already have that handled. To add another field, you just add another block. Something like this should do it:
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"Comment\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[comment dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n",filename] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"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]];
[request setHTTPBody:body];
BTW, you pretty much never need to call NSString's stringWithString: method. If you need a copy, call copy, and otherwise just use the string you already have. NSMutableString's stringWithString: can be useful as an alternative to [[string mutableCopy] autorelease] though.
First of all you should use addValue:forHTTPHeaderField: method of NSMutableURLRequest object to add headers. For example
[request addValue:#"application/octet-stream" forHTTPHeaderField:#"Content-Type"];
[request addValue:[NSString stringWithFormat:#"form-data; name=\"userfile\"; filename=\"%#\"",filename] forHTTPHeaderField:#"Content-Disposition"];
...
For second in your code after all headers are set as I described above you should set POST data with code below.
NSMutableData *body = [[NSMutableData alloc] init];
[body appendData:[[NSString stringWithString:#"name=myimagename.png"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"&data="] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body release];
[request setHTTPBody:body];
But it will work if and only if server supports such format (I mean data=image binary data&name=image name).
I would suggest to use ASIHttp. See ASIFormDataRequest