嘿我在尝试将文件从文件目录上传到Firebase存储,但它根本就没有上传它们...我做了4天的研究并观看了每一个教程,但我不知道为什么这些文件不上传:
我的初始化程序只抓取一个文件并将其传递给实际的上传方法:
func initUploadToFirebase() {
do {
let file = self.getFile()
if let file = file {
self.uploadFile(file: file)
}
} catch { }
}
一个内部帮助函数,用于检索单个文件(实际上是通过我定义的结构的URL及其文件名)
func getFile() -> File? {
var file: File?
do {
let fileNames: [URL] = try self.fileManager.contentsOfDirectory(at: self.documentsUrl, includingPropertiesForKeys: nil, options: [])
// Simply returns the last found file
for fileName in fileNames {
file = File(url: fileName, filename: fileName.lastPathComponent)
}
} catch {
if file != nil {
return file
}
return nil
}
我传递所有需要数据的实际上传方法:...
func uploadFile(file: File) {
let storage = Storage.storage()
// print(file) prints:
// File(url: file:///private/var/mobile/Containers/Data/Application/82871E8A-9919-4075-B982-9E0A019D0AF1/Documents/file11.json, filename: "file11.json")
let ref = storage.reference().child("path").child("to").child(file.filename)
let uploadTask = ref.putFile(from: file.url, metadata: nil, completion: { (metadata, error) in
if let error = error {
print("upload error \(error.localizedDescription)")
}
if let downloadURLString = metadata?.downloadURL()?.absoluteString {
print("downloadURLString \(downloadURLString)")
}
})
uploadTask.observe(.resume) { (snapshot) -> Void in
print("resume")
}
uploadTask.observe(.pause) { (snapshot) -> Void in
print("pause")
}
uploadTask.observe(.failure) { snapshot in
print("error")
}
uploadTask.observe(.progress) { snapshot in
let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount) / Double(snapshot.progress!.totalUnitCount)
print("percentComplete \(percentComplete)")
}
uploadTask.observe(.success) { snapshot in
print("uploadTask success")
self.removeFileByName(name: file.filename)
self.initUploadToFirebase()
}
}
我定义的结构:
struct File {
var url: URL
var filename: String
init(url: URL, filename: String) {
self.url = url
self.filename = filename
}
}
Sooo,我创建了uploadTask,设置了观察者,但 resume 和 progress 只被调用一次......为什么? 上传只是停止,到目前为止我没有解释。
我在最大程度上调试了调试信息,但到目前为止还没有有用的输出......
有人可以帮助我吗?
谢谢和问候!