如何使用Alamofire和base64EncodedString上传文件?

时间:2017-05-16 09:40:40

标签: ios file-upload swift3 alamofire

如何使用Alamofire.upload在服务器上上传base64EncodedString?

我的上传文件网址接受base64中的文件。 当上传完成以JSON格式发送响应时。 如何上传和阅读回复?

func uploadWithAlamofire() {

    let image = #imageLiteral(resourceName: "cartest")

    let imageData:NSData = UIImagePNGRepresentation(image)! as NSData

 // let imagencoded:String = imageData.base64EncodedString(options: [])

    let params = ["token": "454545sddsddf",
                  "filepath": "/",
                  "filearea": "draft"]

    Alamofire.upload(multipartFormData: { multipartFormData in
        if let imageData = UIImageJPEGRepresentation(image, 1) {
        multipartFormData.append(imageData, withName: "filecontent", fileName: "filetest.jpg", mimeType: "image/jpg")
        }

        for (key, value) in params {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    }, to: "http://myuploadurl.php", method: .post,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.response { [weak self] response in
                        guard let strongSelf = self else {
                            return
                        }
                        debugPrint("DATA RESPOND======== \(response)")
                    }
                case .failure(let encodingError):
                    print("error:\(encodingError)")
                }
    })
}

0 个答案:

没有答案