如何在swift2.0中将.pdf文件转换为base64字符串?

jecbmhm3  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(221)

我必须在swift中上传文档,我必须使用iCloud进行此操作,这样我就可以使用icloud将文件上传到我的服务器

ie3xauqp

ie3xauqp1#

斯威夫特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))
oxalkeyp

oxalkeyp2#

拖动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)
ssgvzors

ssgvzors3#

let filePath = "" // real path of the pdf file
let fileData = NSData(contentsOfFile: path)
let fileStream = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) // base64 string
bzzcjhmw

bzzcjhmw4#

你会在哪里选择文件,该文件路径必须给出:
1.首先生成base64字符串,然后使用它
1.然后生成字节
哪一个舒服都用得上。

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 { }

相关问题