更新帖子请求alamofire

时间:2016-06-10 02:56:48

标签: ios swift

我使用Alamofire将带有参数(全名,用户名,电子邮件)的图像上传到我的服务器,并使用以下代码:

func uploadImageData(imageView: UIImageView, fullNameToPass: String, userNameToPass: String, emailToPass: String){
    //parameters
    let parameters = [
        "full_name"  : fullNameToPass,
        "user_name"    : userNameToPass,
        "email"    : emailToPass
    ]

    let URL = "https://webiste/add_user.php"
    profileData.fetchFromDatabase()
    let filename = "\(profileData.getEmail()).jpg"

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in
        if  let imageData = UIImageJPEGRepresentation(imageView.image!, 0.6) {
            multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: filename, mimeType: "image/jpg")
        }
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
            print("success")
            upload.responseJSON { response in
            print(response.request)  // original URL request
            print(response.response) // URL response
            print(response.data)     // server data
            print(response.result)   // result of response serialization

            if let JSON = response.result.value {
                print("JSON: \(JSON)")
            }
                }

        case .Failure(let encodingError):
                print("Failure:\(encodingError)")
            }
    })
}

此部分有效,全部保存。 现在我希望用户能够编辑和更新配置文件。我使用以下但它不起作用,你能告诉我为什么吗?:

func updateImageData(imageView: UIImageView, fullNameToPass: String, userNameToPass: String, emailToPass: String, userID: String){
    //parameters
    let parameters = [
        "full_name"  : fullNameToPass,
        "user_name"    : userNameToPass,
        "email"    : emailToPass,
        "userID" : userID
    ]

    let URL = "https://webiste/edit_user.php"
    profileData.fetchFromDatabase()
    let filename = "\(profileData.getEmail()).jpg"

    print("all\(fullNameToPass), \(userNameToPass), \(emailToPass), \(userID), filename: \(filename)")

    Alamofire.upload(.POST, URL, multipartFormData: {
        multipartFormData in
        if  let imageData = UIImageJPEGRepresentation(imageView.image!, 0.6) {
            multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: filename, mimeType: "image/jpg")
        }
        for (key, value) in parameters {
            multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
        }
        }, encodingCompletion: {
            encodingResult in

            switch encodingResult {
            case .Success(let upload, _, _):
                print("success")
                upload.responseJSON { response in
                    print(response.request)  // original URL request
                    print(response.response) // URL response
                    print(response.data)     // server data
                    print(response.result)   // result of response serialization

                    if let JSON = response.result.value {
                        print("JSON: \(JSON)")
                    }
                }

            case .Failure(let encodingError):
                print("Failure:\(encodingError)")
            }
    })
}

0 个答案:

没有答案