如何使用表单数据或任何其他方法在swift中上传PDF文件

时间:2018-04-05 06:43:47

标签: swift file-upload form-data

我们可以使用swift中的表单数据上传pdf / doc文件吗?或者任何其他上传方式。如果可能的话请提供工作实例,因为我是这项技术的新手。

1 个答案:

答案 0 :(得分:0)

很高兴使用优秀的Alamofire库上传任何文档(例如pdf文件)。下面的代码将解释如何使用alamofire上传文件

Alamofire.upload(
        multipartFormData: {
            multipartFormData in

            if let urlString = urlBase2 {
                let pdfData = try! Data(contentsOf: urlString.asURL())
                var data : Data = pdfData

                multipartFormData.append(pdfData, withName: "pdfDocuments", fileName: namePDF, mimeType:"application/pdf")
                for (key, value) in body {
                    multipartFormData.append(((value as? String)?.data(using: .utf8))!, withName: key)
                }

                print("Multi part Content -Type")
                print(multipartFormData.contentType)
                print("Multi part FIN ")
                print("Multi part Content-Length")
                print(multipartFormData.contentLength)
                print("Multi part Content-Boundary")
                print(multipartFormData.boundary)
            }
    },
        to: url,
        method: .post,
        headers: header,
        encodingCompletion: { encodingResult in

            switch encodingResult {

            case .success(let upload, _, _):
                upload.responseJSON { response in
                    print(" responses ")
                    print(response)
                    print("Responses ended")

                    onCompletion(true, "Something went wrong", 200)

                }
            case .failure(let encodingError):
                print(encodingError)
                onCompletion(false, "Something went wrong", 200)
            }
    })