Note that there are some explanatory texts on larger screens.

plurals
  1. POSwitch from Synchronous request to Asynchronous request with image upload?
    primarykey
    data
    text
    <p>I need some help, i have been searching around and cant find the correct answer im looking for.</p> <p>I am uploading images and videos to my server via php, When im uploading the video or image, i want to be able to show a progress view, i have been told that the only way to do this is to use asynchronous instead of synchronous. I have been looking at ways to set up this, but cant really seem to find a good tutorial that will help me with what im trying to accomplish.</p> <p>Here is some code:</p> <pre><code>- (void)post:(NSData *)fileData { NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:[self saveUserLogin]]; int test; NSString *string = [array objectAtIndex:3]; test = [string intValue]; test++; NSData *videoData = fileData; NSString *urlString = [[NSString alloc]initWithFormat:@"http://www.site.com/members/uploadMovie.php?&amp;username=%@", [array objectAtIndex:0]]; 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]]; NSString *postName = [[NSString alloc]initWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vid%i.mov\"\r\n", test]; [body appendData:[[NSString stringWithString:postName] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:videoData]]; [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]; NSLog(@"%@", returnString); NSArray *values = [[NSArray alloc] initWithObjects: [array objectAtIndex:0],[array objectAtIndex:1], [array objectAtIndex:2], [NSString stringWithFormat:@"%i", test], nil]; [values writeToFile:[self saveUserLogin] atomically:YES]; [self.delegate didFinishController:self]; } </code></pre> <p>this is some code that im using to send a video. filedata is a paramater being passed in with the video data. I want to animate a UIProgressview for the upload progress. I have also heard that apple likes people to use asynchronous anyways. If someone could please help me set up asynchronous instead of what i have, i would be really grateful. Please be specific if you reply, like to what i need to import what delegates, methods. etc.</p> <p>Thank you very much :)</p> <p>EDIT:</p> <p>This is what it looks like now:</p> <pre><code>- (void)post:(NSData *)fileData { NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:[self saveUserLogin]]; int test; NSString *string = [array objectAtIndex:3]; test = [string intValue]; test++; NSData *videoData = fileData; NSString *urlString = [[NSString alloc]initWithFormat:@"http://www.site.com/members/uploadMovie.php?&amp;username=%@", [array objectAtIndex:0]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30]; 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]]; NSString *postName = [[NSString alloc]initWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"vid%i.mov\"\r\n", test]; [body appendData:[[NSString stringWithString:postName] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:videoData]]; [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]; NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; if (connection) { NSLog(@"connected"); responceData = [NSMutableData data]; } else{ NSLog(@"error"); } // NSLog(@"%@", returnString); NSArray *values = [[NSArray alloc] initWithObjects: [array objectAtIndex:0],[array objectAtIndex:1], [array objectAtIndex:2], [NSString stringWithFormat:@"%i", test], nil]; [values writeToFile:[self saveUserLogin] atomically:YES]; // [self.delegate didFinishController:self]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responceData appendData:data]; } - (void) connectionDidFinishLoading:(NSURLConnection *)connection { NSString* responseString = [[NSString alloc] initWithData:responceData encoding:NSUTF8StringEncoding]; NSLog(@"result: %@", responseString); } - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"error - read error object for details"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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