title: Uploading multiple image files with Swift
body: <p>Im using the following code to upload a single image to a server (with parameters), However I've been trying to upload multiple images, but could not get it to work</p>
  
  <p>Creating NSData objects (of images saved in temp directory). Using netdata class <a href="https://github.com/nghialv/Net/blob/master/Net/NetData.swift" rel="nofollow">here</a></p>
  
  <pre><code>    func uploadData(){
      //create image data objects
      let filemanager:NSFileManager = NSFileManager()
      let files = filemanager.enumeratorAtPath(tempPicPath)
      var counter:Int = Int()
      while let file: AnyObject = files?.nextObject() {
          imagePathCollection.append(tempPicPath + "/" + (file as NSString))
      }
  
      //convert to data objects ()
      for path in imagePathCollection{
          var image: UIImage? = UIImage(contentsOfFile: path)
          if image != nil {
              var sizeOfImage = image?.size
              var resizedImage = RBSquareImageTo(image!, sizeOfImage!)
              var imageData = UIImageJPEGRepresentation(resizedImage, 75)
              imageDataCollection.append(NetData(data: imageData!, mimeType:MimeType(rawValue: "image/jpeg")!, filename: "myImage.jpeg"))
          }
      }
  
      if(imageDataCollection.count &gt; 1){
          for (index, element) in enumerate(imageDataCollection) {
              multipleImageUpload(imageDataCollection[index])
          }
      }else{
          singleImageUpload(imageDataCollection[0])
      }
  }
  </code></pre>
  
  <p>Using Alamofire to upload</p>
  
  <pre><code>        func extrasImageUpoload(urlRequest:(URLRequestConvertible, NSData))-&gt;Request{
      let request = Alamofire.upload(urlRequest.0, urlRequest.1)
          .progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
              println("progress : \(totalBytesWritten) / \(totalBytesExpectedToWrite)")
      }
      return request
  }
  </code></pre>
  
  <p>Generating the Alamofire request</p>
  
  <pre><code>    func urlRequestWithComponents(parameters:NSDictionary, uploadType:String) -&gt; (URLRequestConvertible, NSData) {
  
      var mutableURLRequest:NSMutableURLRequest = NSMutableURLRequest()
  
      //set url type
      if(uploadType == "extra"){
          mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/extra")!)
      }else if(uploadType == "checkList"){
          //uploadUrl = "ticket/mark"
          mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/mark")!)
      }
  
      // create url request to send
      //var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: baseURL + "ticket/extra")!)
      mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
      //let boundaryConstant = "myRandomBoundary12345"
      let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"
      let contentType = "multipart/form-data;boundary="+boundaryConstant
      //let pgToken = "c9049df83e8bfd7a3dfaef279cdb74478330ff2a"
      mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
      mutableURLRequest.setValue(self.token, forHTTPHeaderField: "PG-Auth-Token")
  
      // create upload data to send
      let uploadData = NSMutableData()
  
      // add parameters
      for (key, value) in parameters {
  
          uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
  
          if value is NetData {
              // add image
              var postData = value as NetData
              //uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!
              var filenameClause = " filename=\"\(postData.filename)\""
              let contentDispositionString = "Content-Disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"
              let contentDispositionData = contentDispositionString.dataUsingEncoding(NSUTF8StringEncoding)
              uploadData.appendData(contentDispositionData!)
  
  
              // append content type
              //uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
              let contentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"
              let contentTypeData = contentTypeString.dataUsingEncoding(NSUTF8StringEncoding)
              uploadData.appendData(contentTypeData!)
              uploadData.appendData(postData.data)
  
          }else{
              uploadData.appendData("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
          }
      }
      uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
  
      // return URLRequestConvertible and NSData
      return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
  }
  </code></pre>
  
  <p>The parameters array along with the NSData collection as below</p>
  
  <pre><code> var params = ["pic":imageData,"ticketid":ticketID, "appversion":Utilities().getAPPVersion(), "description":"bla bla bla", "cost":50]
  </code></pre>
  
  <p>"imageData" is a collection of NSData objects of the images. Code works fine but the NSData array of the images received as an empty array. However rest of the parameters including  "appversion" and "description" will be received fine </p>