2016-09-27 43 views
0

我需要上傳帶有一些額外文本的圖像陣列。我用Alamofire這樣的:alamofire上傳圖像陣列不起作用

let headers = ["Authorization": "Bearer \(userToken)"] 
      Alamofire.upload(.POST, "url", headers: headers, multipartFormData: { multipartFormData in 

       if let post = post?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { 
        multipartFormData.appendBodyPart(data: post, name: "name") 
       } 
       if let pictures = pictures { 
        for image in pictures { 
         if let imageData = UIImageJPEGRepresentation(image, 1)?.base64EncodedDataWithOptions(.Encoding64CharacterLineLength) { 
          multipartFormData.appendBodyPart(data: imageData, name: "pictures", fileName: "pictures", mimeType: "image/jpeg") 
         } 
        } 
       } 
       if let pictureText = pictureText?.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) { 
        multipartFormData.appendBodyPart(data: pictureText, name: "picture_text") 
       } 

       }, encodingCompletion: { encodingResult in 
        switch encodingResult { 
        case .Success(let upload, _, _): 
         upload.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in 
          DDLogDebug("Uploading images for status post \(totalBytesWritten)/\(totalBytesExpectedToWrite)") 
         } 
         upload.responseJSON { (response) in 
          let json = response.result.value 
          DDLogDebug("Status post complete \(json)") 
          dispatch_async(dispatch_get_main_queue(),{ 
           handler(result: json, error: nil) 
          }) 
         } 
        case .Failure(let encodingError): 
         DDLogError("Failed to post: \(encodingError)") 
         handler(result: nil, error: NSError(domain: "failed response", code: 123, userInfo: nil)) 
        } 
       } 
      ) 
     } 

現在的問題是,服務器要像陣列,並給我們的錯誤中缺少的multipart/form-data發佈的數據邊界在未知在線0,所以有什麼上傳代碼錯誤,但我無法弄清楚什麼。在郵遞員這只是一個數組,它工作正常。我想,當我循環圖片並給出相同的名稱時,它就像一個數組。

回答

0

我忘了索引添加到文件名,這是解決

if let pictures = pictures { 
       for (index, image) in pictures.enumerate() { 
        if let imageData = UIImageJPEGRepresentation(image, 1) { 
         multipartFormData.appendBodyPart(data: imageData, name: "pictures[\(index)]", fileName: "picture", mimeType: "image/jpeg") 
        } 
       } 
      }