【问题标题】:alamofire upload array of images doesn't workalamofire 上传图像数组不起作用
【发布时间】:2016-09-27 16:33:58
【问题描述】:

我需要上传一组带有一些额外文字的图像。我是这样使用 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))
                    }
                }
            )
        }

现在的问题是服务器想要一个图像数组,并在第 0 行的 Unknown 中向我们提供错误 Missing boundary in multipart/form-data POST data 。所以上传代码有问题,但我不能弄清楚什么。在邮递员中,它只是一个数组,它工作正常。我想当我循环浏览图片并给出相同的名称时,它会像一个数组一样。

【问题讨论】:

    标签: ios swift2 alamofire


    【解决方案1】:

    我忘了给文件名添加索引这是解决办法

      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")
                        }
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2017-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多