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

标签 swift file-upload form-data

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

最佳答案

使用优秀的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)
            }
    })

关于swift - 如何使用表单数据或任何其他方法快速上传 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665732/

相关文章:

ios - 使用 UIView 水平裁剪图像

javascript - 通过iframe上传,IE8/9访问被拒绝(跨域)

mysql - 在 Laravel 中上传和检索图像

javascript - 如何使用javascript在文件上传中获取不带扩展名的文件名

javascript - Axios FormData() 获取空对象

javascript - 将 Blob 对象转换为文件,对于 Ms Edge

javascript - 从javascript中的表单数据中删除数据

swift - 在 SQLite 数据库中返回行时出现 "unexpectedly found nil while unwrapping an Optional value"错误

swift - 如何在扩展 SequenceType 的协议(protocol)中限制 Generator.Element 的类型

ios - UICollectionViewCell 和 UICollectionView 的高度相等