我正在为几行令人尴尬的代码而苦苦挣扎。我已经阅读了很多答案,但似乎没有一个有效。在我的函数中,我从选择的文档中获取文件路径:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let urlString = url.path
let fileName = url.lastPathComponent
let ref = DataService.instance.imagesStorageRef.child("\(fileName)")
var contents = try? Data(contentsOf: url)
print("contents: \(contents)")
let uploadTask = ref.putData(contents! as Data, metadata: nil, completion: { (metadata, error) in ...}
}
file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/nutella.jpg contents: nil
然后我尝试获取该文件路径的内容,以便将其传递给上传函数,该函数将其上传到 firebase。但是,无论我如何尝试获取该内容,我总是得到 nil 值。请引导我走向正确的方向。
另一种可能对您有所帮助的方法。为此,您需要将该文档转换为数据形式,然后您可以将其发送到服务器
我在这里粘贴示例代码..
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let fileURL = url as URL
print("The Url is : \(fileURL)")
let fileNameWithoutExtension = fileURL.pathExtension
print("fileNameWithoutExtension: \(fileNameWithoutExtension)")
do {
let data = try Data(contentsOf: fileURL)
print("data=\(data)")
//TODO: call upload API
self.sendAttachments()
}
catch {/* error handling here */}
}
关于ios - 从路径获取文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518548/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |