Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Monaj

macrumors regular
Original poster
May 24, 2009
193
0
Hi All

I am using an API to upload a file to the server How can I update the progress indicator on file uploading with showing proper percentage of uploaded data?

Code which I am using for file upload


Code:
NSString *tempMsg,*stringBoundary,*contentType,*returnResult;
NSURL *cgiUrl ;
NSMutableURLRequest *postRequest;
NSError *error;
NSData *searchData;
NSHTTPURLResponse *response;
NSMutableData *postBody;
NSString *temp1,*attachedFile=@"",*originalAttachedFile=@"";
temp1=[[[attachedfileArray objectAtIndex:0] objectForKey:@"fileName"] lastPathComponent];
if([originalAttachedFile isEqualToString:@""]){
	originalAttachedFile=temp1;
}else {
	originalAttachedFile=[NSString stringWithFormat:@"%@,%@",originalAttachedFile,temp1];
}
 
NSRange rangeForDot=[temp1 rangeOfString:@"."];
temp1=[temp1 substringFromIndex:rangeForDot.location];
 
cgiUrl = [NSURL URLWithString:@"https://www.keysoftwareservices.com/API/rtcs_API.php"];
postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
			[postRequest setHTTPMethod:@"POST"];
 
stringBoundary = [NSString stringWithString:@"0000RCSIMGUploadxxxxxx"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
 
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"code\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:ans] dataUsingEncoding:NSUTF8StringEncoding]];
 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"action\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"uploadfile"] dataUsingEncoding:NSUTF8StringEncoding]];
 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"path\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"/API/attachments/"] dataUsingEncoding:NSUTF8StringEncoding]];
 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", [[[attachedfileArray objectAtIndex:l] objectForKey:@"fileName"] lastPathComponent]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: image/%@\r\n\r\n",temp1] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"Image type in post method.... %@",[NSString stringWithFormat:@"Content-Type: image/%@\r\n\r\n",temp1]);
[postBody appendData:[NSData dataWithContentsOfFile:[[attachedfileArray objectAtIndex:l] objectForKey:@"fileName"]]];
 
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
 
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
 
if([error code]==0){
}else {
NSBeginAlertSheet(@"Fail to attach file !",@"Ok",nil,nil,msgWindow,self,@selector(sheetDidEndShouldDelete4:returnCode4:contextInfo4:),NULL,msgWindow,@"Please check internet connnection");
return;
}
 
NSString* imageName;
imageName = [[NSString alloc] initWithData:searchData encoding:NSASCIIStringEncoding];
 
NSLog(@"retrun Result of Upload Photo.. %@",imageName);

Please guide me on how should I show the progress indicator while uploading the file


Thanks
Monaj
 
NSURLConnection sendSynchronousRequest: blocks the current thread until it completes. Either run this is a separate thread and update the UI on the main thread or use the asychronous methods of NSURLConnection as clearly described in the documentation.
 
How should I use it in different thread I need its output for next bit of code
 
Then you're next bit of code will have to wait for the thread to complete. I suggest you take a big step back and actually design this before touching the keyboard.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.