使用Alamofire

时间:2018-04-17 12:29:47

标签: ios swift alamofire swift4

我正在使用Swift4,我已使用此代码将图像上传到服务器

class func uploadMultipleAdvertisementImage(photos: UIImage, completion: @escaping (_ error: Error?, _ sucess: Bool, _ image_id: Int)-> Void) {

    let url = URLs.uploadImages

    var images = [Data]()
    Alamofire.upload(multipartFormData: { (form: MultipartFormData) in



       if let data = UIImageJPEGRepresentation(photos , 0.5) {
            form.append(data, withName: "images", fileName: "photo.jpeg", mimeType: "image/jpeg")
        }

    }, usingThreshold: SessionManager.multipartFormDataEncodingMemoryThreshold, to: url, method: .post, headers: nil) { (result: SessionManager.MultipartFormDataEncodingResult) in

        switch result {
        case .failure(let error):
            print(error)
            completion(error, false, 0)

        case .success(request: let upload, streamingFromDisk: _, streamFileURL: _):

            upload.uploadProgress(closure: { (progress: Progress) in
                print(progress)
            })
                .responseJSON(completionHandler: { (response: DataResponse<Any>) in

                    switch response.result
                    {
                    case .failure(let error):
                        print(error)
                        completion(error, false, 0)

                    case .success(let value):
                        let json = JSON(value)
                        print(json)
                        if(json["msg"] == "image uploaded successfully") {
                            let image_id = json["image_id"].int ?? 0
                            print("hiiiiiiiiii", image_id)
                            completion(nil, true, image_id)
                        }
                    }

                })
        }

    }
}

它成功地运作了, 但我想将图像数组上传到名为“images”的服务器。 有没有办法将图像数组上传到服务器而不是上传一个?

3 个答案:

答案 0 :(得分:0)

没什么需要做的。 添加你的

if let data = UIImageJPEGRepresentation(photos , 0.5) {
            form.append(data, withName: "images", fileName: "photo.jpeg", mimeType: "image/jpeg")
        }

进入For循环的代码,其余的东西AlamoFire会照顾。

答案 1 :(得分:0)

假设您在函数中获得images: [UIImage]作为参数。然后你只需要将所有图像的数据附加到多部分表格中,只需更新每个图像的fileName即可。

将代码行更新为:

for i in 0..<images.count {
    if let data = UIImageJPEGRepresentation(photos[i], 0.5) {        
        form.append(data, withName: "images", fileName: "photo\(i).jpeg", mimeType: "image/jpeg")
    }
}

答案 2 :(得分:0)

要告诉服务器端您正在发送图像或视频阵列,必须在参数名称后附加开闭方括号 '[]' 。祝你好运,编码愉快。

class Name: Object, Mappable {
@objc dynamic var id: Int = 0
@objc dynamic var fisrtname: String? = ""
@objc dynamic var lastname: String? = ""
@objc dynamic var Address: Address? = nil
override static func primaryKey() -> String? {
    return "id"
}

required convenience init?(map: Map) {
    self.init()
}

func mapping(map: Map) {
    id <- map["id"]
    fisrtname <- map["fisrtname"]
    lastname <- map["lastname"]
    Address <- map["Address"]
}

class Address: Object {
    @objc dynamic var id: Int = 0
    @objc dynamic var city: String? = ""
    @objc dynamic var phone: Int? = ""
    @objc dynamic var street: String? = ""

    override static func primaryKey() -> String? {
        return "id"
    }

    required convenience init?(map: Map) {
        self.init()
    }

    func mapping(map: Map) {
        id <- map["id"]
        city <- map["city"]
        phone <- map["phone"]
        street <- map["street"]

    }
}