我必须在 swift 中上传文档,我必须使用 iCloud 来执行此操作,以便我可以使用 icloud 将文件上传到我的服务器
斯威夫特 3.x
let filePath = ... //URL of the PDF
let fileData = try? Data.init(contentsOf: filePath)
let fileStream: String? = fileData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
将pdf文件拖到项目或之前的五个路径中保存
let url1 = Bundle.main.url(forResource: "pdffilename without extension", withExtension: "pdf")
let one1 = NSData(contentsOf: url1!)
let pdfstring:String = one1!.base64EncodedString(options: .endLineWithLineFeed)
print(pdfstring)
let filePath = "" // real path of the pdf file
let fileData = NSData(contentsOfFile: path)
let fileStream = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) // base64 string
您要在哪里选择文件,必须给出该文件路径:
首先生成base64字符串就可以使用了
然后生成字节
哪个舒服就可以用。
do {
let fileData = try Data.init(contentsOf: filePath!)
let fileStream:String = fileData.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))
let decodeData = Data(base64Encoded: fileStream, options: .ignoreUnknownCharacters)
body.append(decodeData!)
}
catch { }