Use of NSOperation in upload HTTP code - objective-c

I need to implement a subclass of NSOperation which does a file upload to a HTTP server and with an option that the user can cancel the file upload during operation.
Here is the code at the moment:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:filename];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:20];
[request setHTTPMethod:#"POST"];
NSString *boundary = #"------WebKitFormBoundary4QuqLuM1cE5lMwCy";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
NSString *FileParamConstant = #"uploadFile";
NSData *imageData = [[NSData alloc] initWithContentsOfFile:imagePath];
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:11];
[parameters setValue:#"Value" forKey:#"Server_required_param"];
NSString *urlString = #"http://www.omeuendereco/uploadFile.php";
for (NSString *param in parameters) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"%#\"\r\n", FileParamConstant, filename] 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]];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:urlString]];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
delegate:up
startImmediately:YES];
[connection start];
the problem is that i have to implement a NSOperationqueue in this code and i dont know how.
Then with the subclass of NSOperation i will be able to cancel the upload operation.

What the hell is your problem ? If you don't know how to make a subclass - learn Objective-C.
If I don't understand you (and there's some sophiscated problem, but I don't think so), you've got two solutions.
CREATE THAT SUBCLASS and then make an instance of NSOperationQueue, add an instance of that subclass to the queue.
Use AFNetworking or MKNetworkKit(MKNetworkKit uses NSOperation and NSOperationQueue)

Related

Objective c uploading multiple files to server

In my app i need to upload a audio file and xml file to server. I successfully post audio file. But i don’t know how to send two files in parallel. Anyone please help me.
NSString *soundFilePath = [[self GetDocumentDirectory]
stringByAppendingPathComponent:audioDet.audioName];
NSMutableData *file1Data = [[NSMutableData alloc] initWithContentsOfFile:soundFilePath];
//uploads/
NSString *urlString = #"http://192.168.1.99/projects/fileUpload/upload.php";
NSString *filename = audioDet.audioName;
NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init] ;
[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"];
[request setValue:#"Crystal" forHTTPHeaderField:#"ClientApp"];
[request setValue:#"1.0" forHTTPHeaderField:#"ClientVersion"];
[request setValue:#"617272656E64616C65445652" forHTTPHeaderField:#"ClientCredential"];
[request setValue:audioDet.audioName forHTTPHeaderField:#"Target-file-name"];
[request setValue:[NSString stringWithFormat:#"%#",audioDet.audioFileSize] forHTTPHeaderField:#"Target-file-length"];
NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// NSString * value1 = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; name=\"%#\"\r\n", filename];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n", filename ] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:file1Data]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
Thanks,
AKS
Store the multiple files in an array and iterate the same using for loop to send multiple files in the server.

Xcode Upload File (Image) to FTP site

i want to upload a file, an image, to a website. I have read the SimpleFTPSample from Apple. It´s work with the TestPhotos but it´s too complicated. Can someone post a simple Code, where i can put my URL in it? I have used BlackRaccoon and ASIHTTPRequest but it doesn´t work.
Thanks
What worked easiest for me was to write a small php script and upload the file to that. The php isn't that sophisticated but it works
be sure to replace the urls with your site name
upload.php
<?php
$uploaddir = 'Images/';
$file = basename($_FILES['userfile']['name']);
$uploadfile = $uploaddir . $file;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "transfer successful at: http://YOURWEBSITE.COM/{$uploaddir}/{$file}";
}
?>
obj-c
NSString *imageString = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userfile\"; filename=\"%#\"\r\n", YOURIMAGE];
NSData *imageData = UIImageJPEGRepresentation(newimageGlobal, 0.4);
NSString *urlString = #"http://YOURWEBSITE.COM/upload.php";
NSLog(#"upload url%#", urlString);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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:imageString ] 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];

How to send JSON request to service with parameters in Objective-C

I'm creating a iPhone app, and im trying to figure out how to create a JSON request to the webservice that contains parameters. In Java this would look like this
HashMap<String, String> headers = new HashMap<String, String>();
JSONObject json = new JSONObject();
json.put("nid", null);
json.put("vocab", null);
json.put("inturl", testoverview);
json.put("mail", username); // something#gmail.com
json.put("md5pw", password); // donkeykong
headers.put("X-FBR-App", json.toString());
The header has to contain a JSON object and "X-FBR-App" before the service recognizes the request. How would this be implemented in Objective-C ?
USE Post method.try this
NSString *method = #"addProduct";
NSString *jsonstring=#"http://yourdomain.com/product_review_api/product-review.php?";
NSString *urlString = [NSString stringWithFormat:#"%#",jsonstring];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
// image file
NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
// parameter method
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// parameter categoryid
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"category_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[selectedID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
// 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);
NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
NSLog(#"profile=%#", profile);
you can append all the parameters like this.
JSON isn't baked into iOS, but if you look at www.JSON.org there are a number of Objective-C frameworks that provide the required functionality. I have no experience with any of them so I can't make recommendations, but it is a good start to finding a solution.
Also look at this SO question post-data-through-json-webservices-in-iphone

How to upload multiple images without using ASIHTTPRequest

I want to upload two images to the server with following code can any one please help me, here is my code for post data
- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];
NSString *boundary = #"-------1234567";
[mutableRequest setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary] forHTTPHeaderField:#"Content-Type"];
[mutableRequest setHTTPMethod:#"POST"];
NSMutableData *postbody = [NSMutableData data];
for(NSString *key in options)
{
NSString *value = [options objectForKey:key];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
if ([imageName1 isEqualToString:#"picture1.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture1\"; filename=\"%#\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data1]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
//******************* Append 2nd Image ************************/
if ([imageName2 isEqualToString:#"picture2.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture2\"; filename=\"%#\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data2]];
//******************* Append 2nd Image ************************/
[mutableRequest setHTTPBody:postbody];
urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
if (self.urlConnection) {
self.dataXml = [NSMutableData data];
}
self.urlConnection = nil;
[mutableRequest release];
[pool release];
}
One solution to see what is wrong:
make a new project, using ASIHttpRequest
Get a web debugging proxy like Charles
Turn on Charles (or whatever), do a multi-file upload that succeeds.
Record data from HTTP traffic to server.
Now run your own code. Record that traffic. What is different from
your traffic to the working traffic? Makes yours IDENTICAL in all
respects, and how can it not work?
You can also replace step (1) with a curl command line that uploads multiple files to a server and record that traffic, but I'm not versed enough in curl to say off the top of my head what that command would look like.
- (NSString*) nameValString: (NSDictionary*) dict {//example CHLOVA_FORM_BOUNDARY is #"aaa"
NSArray* keys = [dict allKeys];
NSString* result = [NSString string];
int i;
for (i = 0; i < [keys count]; i++) {
result = [result stringByAppendingString:
[#"--" stringByAppendingString:
[CHLOVA_FORM_BOUNDARY stringByAppendingString:
[#"\r\nContent-Disposition: form-data; name=\"" stringByAppendingString:
[[keys objectAtIndex: i] stringByAppendingString:
[#"\"\r\n\r\n" stringByAppendingString:
[[dict valueForKey: [keys objectAtIndex: i]] stringByAppendingString: #"\r\n"]]]]]]];
}
return result;
}
- (void)sendPhoto{
NSString *param = [self nameValString:dic];//dic is post info like {lat=36;lng=120;}
NSString *footer = [NSString stringWithFormat:#"\r\n--%#--\r\n", CHLOVA_FORM_BOUNDARY];
param = [param stringByAppendingString:[NSString stringWithFormat:#"--%#\r\n", CHLOVA_FORM_BOUNDARY]];
param = [param stringByAppendingString:#"Content-Disposition: form-data; name=\"pic\";filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"];
NSData *jpeg = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:_path], 0.55);
//NSLog(#"jpeg size: %d", [jpeg length]);
NSMutableData *data = [NSMutableData data];
//img one
[data appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg];
[data appendData:[footer dataUsingEncoding:NSUTF8StringEncoding]];
//img two
[data appendData:[param2 dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg2];
[data appendData:[footer2 dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", CHLOVA_FORM_BOUNDARY];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:#"POST"];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:data];
//then do sending
}

UIIMagePickerController & NSURLConnection

UIImagePickerController freezes while I think the Image is being compressed or being sent by NSURLConnection although I'm not convinced it's the later.
What I want is, all this to be done in the background. Instead when I pick a photo from the library, the Screen freezes for what feels like forever. This isn't optimal, obviously. What approach should I take here?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
int i = 0;
NSString *uniquePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/selectedImage.png"];
while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath])
{
uniquePath = [NSString stringWithFormat:#"%#/%#-%d.%#", [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"], #"selectedImage", ++i, #"png"];
}
NSLog(#"Writing selected Image to Documents Folder, %#", uniquePath);
dataForPNGFile = UIImagePNGRepresentation(img);
if (!dataForPNGFile) return NO;
[UIImagePNGRepresentation(img) writeToFile:uniquePath atomically:YES];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
[[self parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
NSString *urlString = #"http://localhost:3000/photos";
NSURL *url = [NSURL URLWithString:urlString];
NSString *boundary = #"----1010101010";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSString *photoPath = [[NSBundle mainBundle] pathForResource:#" my-photo " ofType:#"jpg"];
NSData *photoData = [NSData dataWithContentsOfFile:photoPath];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"photo-description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"testing 123" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%#\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:photoData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"tags\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"random,test,example" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:#"%d", body.length] forHTTPHeaderField: #"Content-Length"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
You should use:
detachNewThreadSelector:toTarget:withObject:
Detaches a new thread and uses the specified selector as the thread entry point.
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
Documentation here: NSThread
This will create a new thread for the operation that is eating up the cpu, so your UI isn't affected remember to create a NSAutoreleasePool for the new thread, as only the main thread gets a NSAutoreleasePool pool by default.
So at the beginning of your selector do:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//and at the end:
[pool drain]: