title: Uploading Audio File using HTTP POST in Objective-C
body: <p>HI,</p>
  
  <p>I have been trying to upload an audio file (sample.wav) to my server using the HTTP POST method by implementing the following code. There is no error connecting to the server but the file is not uploading. I have spent hours to find a solution for this but couldn't find one yet. Here is the code that I have been using to do the task.</p>
  
  <pre><code>     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"wav"];
  
      // NSLog(@"filePath : %@", filePath);
  
  
  
       NSData *postData = [[NSData alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
  
       NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
  
       NSLog(@"postLength : %@", postLength);
  
  
  
       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  
       [request setHTTPMethod:@"POST"];
  
       [request setURL:[NSURL URLWithString:@"http://exampleserver.com/upload.php"]];
  
  
  
       NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
  
       NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  
       [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  
  
  
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  
  
  
       [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  
       [request setHTTPBody:postData];
  
       [request setTimeoutInterval:30.0];
  
  
  
       NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
  
  
  
       if (conn)
  
       {
  
          receivedData = [[NSMutableData data] retain];
  
       } else {
  
          NSLog(@"Connection Failed");
  
       }
  
  
  
       //[request release];
  </code></pre>
  
  <p>Then I am using the delegates methods of NSURLConnection to get response from my server. It does try to connect to the server but there is no response. Can anybody please help me out in this regards. And also here is the simple HTML code that I have been using to upload via simple web browser.</p>
  
  <pre><code>&lt;form action="http://exampleserver.com/upload.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data"  &gt;
  
  
  
                          &lt;input type="file"  name="filename" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>And one last thing we must have to pass the name for input field which is "filename" here in this case. I dont know how to pass this in the objective-c. So please somebody can point me into the right direction. Any kind of help will be greatly appreciated.</p>
  
  <p>regards,</p>
  
  <p>Arslan</p>