title: ios: Uploading mulitpart file to AmazonS3
body: <p>I'm trying to upload a file to Amazon S3 from my iOS app. I'm using AFNetworking for network requests. I have the following code for uploading file to S3:</p>
  
  <pre><code>func uploadFile(filePath: String, uploadUrl: String, returnDict: NSDictionary? = nil) {
      let fileName = filePath.lastPathComponent
      let fileType = fileName.pathExtension
      if fileName != "" &amp;&amp; fileType != "" {
          if let data = NSData(contentsOfFile: filePath) {
              let manager = AFHTTPRequestOperationManager()
  
              manager.requestSerializer.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
              manager.requestSerializer.setValue("\(data.length)", forHTTPHeaderField: "Content-Length")
              let request = manager.requestSerializer.multipartFormRequestWithMethod("PUT",
                  URLString: uploadUrl,
                  parameters: ["Content-Type":"\(fileType)"],
                  constructingBodyWithBlock: { (formData :AFMultipartFormData!) -&gt; Void in
                      formData.appendPartWithFileData(data,
                          name: "file",
                          fileName: uploadUrl.lastPathComponent,
                          mimeType: FileUploader.mimeType[fileType])
                  },
                  error: nil)
  
              let operation = manager.HTTPRequestOperationWithRequest(request,
                  success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -&gt; Void in
                      println(uploadUrl)
                      self.delegate?.fileUploadComplete?(true, returnDict: returnDict)
                  },
                  failure: { (operation: AFHTTPRequestOperation!, error: NSError!) -&gt; Void in
                      println("Error : " + error.description)
                      self.delegate?.fileUploadComplete?(false, returnDict: nil)
                  })
  
              operation.setUploadProgressBlock({ (bytesWritten: UInt,
                  totalBytesWritten: Int64,
                  totalBytesExpectedToWrite: Int64) -&gt; Void in
                      println("Wrote \(totalBytesWritten)/\(totalBytesExpectedToWrite)")
                  })
  
              manager.operationQueue.addOperation(operation)
          }
          else {
              println("File data nil")
              delegate?.fileUploadComplete?(false, returnDict: nil)
          }
      }
      else {
          println("File path not correct")
          delegate?.fileUploadComplete?(false, returnDict: nil)
      }
  }
  </code></pre>
  
  <p>The file gets uploaded completely on S3 but is malformed(/corrupted). If I upload an audio the file doesn't play when I redownload it using the same url. The delegate. methods are just for notifying listener for success or failure. As the file size may be too big for my case I need to do multipart upload.</p>