如何将多个图像上传为数组

时间:2016-06-01 09:38:34

标签: ios arrays swift2.2 alamofireimage

我正在尝试使用POST请求上传多个图像,方法如下:

file[] : photo_1.jpg
file[] : photo_2.jpg
.
.
file[] : photo_n.jpg

文件上传采用数组形式。这就是我想知道的原因

  • 如何修改我的单张图片代码以拍摄多张图片
  • 如何实施图片上传的进度跟踪器

代码:

func submitPhoto(onCompletion: () -> (), onError: ((NSError) -> ())? = nil) {

    guard let endPoint = Data.sharedInstance.submitPhotoEndpoint
        else { print("Empty endpoint"); return }

    let user = Users()

    let Auth_header = [
        "Authorization" : user.token,
        ]

    let image = UIImage(named: "myImage")!


    let parameters = [
            "submission_id": "27",
            "comment": "This is a comment"
    ]

    // Begin upload
    Alamofire.upload(.POST, endPoint, headers: Auth_header, multipartFormData: { multipartFormData in

        // import image to request
        if let imageData = UIImageJPEGRepresentation(image, 1) {
            multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "http://public/uploads/1462026701_ir6.jpg", mimeType: "image/jpg")
        }

        // import parameters
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }

    }, // you can customise Threshold if you wish. This is the alamofire's default value
    encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .Success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .Failure(let encodingError):
            print(encodingError)
        }
    })

    }

0 个答案:

没有答案