Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 22nd, 2012  |  syntax: None  |  size: 3.18 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Uploading a file with ASIFormDataRequest does not work
  2. NSInteger httpStatus;
  3. NSString *httpResponseString;
  4. NSError *httpRequestError;
  5.  
  6. NSArray *paths = [[[NSArray alloc] init] autorelease];
  7. paths =  NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
  8. NSString *documentsDirectory = [paths objectAtIndex:0];
  9. NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"abiliator_basis_de_ar.xml"];
  10.  
  11.  
  12. NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/"];
  13. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
  14. [request setShouldPresentCredentialsBeforeChallenge:NO];
  15. [request setUsername:@"myUserName"];
  16. [request setPassword:@"myPassword"];
  17. [request setDomain:@"myDomainName"];
  18. [request setFile:[NSURL URLWithString:filename] forKey:@"xml"];
  19. [request startSynchronous];
  20. httpRequestError = [request error];
  21. httpResponseString = [request responseString];
  22. httpStatus = [request responseStatusCode];
  23. if (!httpRequestError) {
  24.     httpStatus = [request responseStatusCode];
  25.     NSLog(@"Class %@, Method: %@ - OK login and filetransfer successful '%i'", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
  26. }
  27. else {            
  28.     NSLog(@"Class %@, Method: %@ - Error '%i' occurred sending the http request", self.myClassName, NSStringFromSelector(_cmd), httpStatus);
  29. }
  30.        
  31. rene-stegs-macbook-pro:~ renesteg$ ls -ltr '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml'
  32. -rw-r--r--@ 1 renesteg  staff  1062 22 Mai 13:44 /Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
  33.        
  34. Filename string is: '/Users/renesteg/Library/Application Support/iPhone Simulator/5.1/Applications/417BD791-64BC-48D0-B519-F10C7F617E36/Documents/abiliator_basis_de_ar.xml
  35.        
  36. request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
  37. [request setRequestMethod:@"POST"];
  38. [request setTimeOutSeconds:120];
  39.  
  40. [request setPostFormat:ASIMultipartFormDataPostFormat];
  41. [request addRequestHeader:@"Content-Type" value:@"multipart/form-data"];  
  42. if ([fileManager fileExistsAtPath:filePath] == YES) {
  43.  
  44.     [request setFile:filePath withFileName:@"test.xml" andContentType:@"xml" forKey:@"FieldName"];
  45. }
  46.        
  47. .....
  48. NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/"];
  49. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
  50. [request setShouldPresentCredentialsBeforeChallenge:NO];
  51. [request setUsername:@"myUserName"];
  52. [request setPassword:@"myPassword"];
  53. [request setDomain:@"myDomainName"];
  54. [request setData:[NSData dataWithContentsOfFile:fileName] forKey:@"xml"];
  55. [request startSynchronous];
  56. .....
  57.        
  58. [request setFile:[NSURL fileURLWithPath:filename] forKey:@"xml"];
  59.        
  60. NSURL *myURL = [NSURL URLWithString: @"http://my.url.com/upload.php"];
  61.        
  62. <?php
  63.  $target = "upload/";
  64.  $target = $target . basename( $_FILES['xml']['name']) ;
  65.  $ok=1;
  66.  if(move_uploaded_file($_FILES['xml']['tmp_name'], $target))
  67.  {
  68.     echo "The file ". basename( $_FILES['xml']['name']). " has been uploaded";
  69.  }
  70.  else {
  71.     echo "Error uploading". basename( $_FILES['xml']['name']). " occured";
  72.  }
  73.  ?>