用alamofire上传一个pdf文件



我得到了以下函数,它是从swagger代码gen创建的:

open class func uploadFile(firstname: String, lastname: String, timestamp: Date, file: URL, completion: @escaping ((_ data: ApiResponse?,_ error: Error?) -> Void)) {

在应用程序中,你可以用相机制作图像,图像被转换为pdf文件:

let document = PDFDocument()
let pdfPage = PDFPage(image: unwrapImage)
document.insert(pdfPage!,at: 0)

所以现在我想上传这个文件。但是document.documentURL总是零。虽然我可以在显示器上显示pdf文档。我是否应该将pdf文档保存到临时目录中,以便使用带有url参数的函数?

PDFDocument的属性documentURL仅为get。如果不使用url初始值设定项,它将始终返回nil。您需要的是将pdf文档dataRepresentationwrite的pdf数据获取到临时或永久位置的url。然后你可以上传它的URL。
let document = PDFDocument()
let image = UIImage(named: "imageName.jpg")!
if let pdfPage = PDFPage(image: image) {
document.insert(pdfPage,at: 0)
do {
print(FileManager.default.temporaryDirectory.path)
let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent("pdfName.pdf")
try document.dataRepresentation()?.write(to: fileURL)
// upload your fileURL or copy to a permanent location
} catch {
print(error)
}
}

相关内容

最新更新