如何使用iOS将pdf文件上传到Firebase

时间:2018-10-25 06:10:59

标签: ios objective-c swift firebase-storage

我可以使用pdfSwift文件上传到Firebase吗? 如果可以的话,请与我分享代码。

我正在使用以下代码

let proofRef = filesstorgRef.child(timestamp)

let uploadTask = proofRef.putData(data, metadata: nil, completion: { (metadata, error) in
   if error != nil {
       //print("Failed to upload image:", error)
       return
   }
   if let fileUrl = metadata?.downloadURL()?.absoluteString {
       completion(fileUrl)
   }
})
uploadTask.observe(.progress) { (snapshot) in
   if (snapshot.progress?.completedUnitCount) != nil {
       print("ImageUploadingPerCent===  \(String(describing: snapshot.progress?.completedUnitCount))")
   }
}
uploadTask.observe(.success) { (snapshot) in
   print("ImageUploading Success")
}
uploadTask.observe(.failure) { (snapshot) in
   LoadingView.sharedInstance.visible(visible: false)
   print("ImageUploading failure")
}

预先感谢

1 个答案:

答案 0 :(得分:2)

// Get the default app firebse storage reference

let FIRStorage = Storage.storage()

// reference of the storage
let storageRef = FIRStorage.reference()

// You have to get the file URL from disk or anywhere

let filePath = Bundle.main.path(forResource: "mypdf", ofType: "pdf")
let filePathURL = URL(fileURLWithPath: filePath!)

// Create a reference/Path on firebase database, where you want to upload your file
let fileRef = storageRef.child("firebase path with filename")

// from this you cant upload the file on fileRef path
let uploadTask = fileRef.putFile(from: filePathURL, metadata: nil) { metadata, error in
    guard let metadata = metadata else {
        // error!
        return
    }
    let metadataSize = metadata.size
    // get the download url of this file
    fileRef.downloadURL { (url, error) in
        guard let downloadURL = url else {
            // error!
            return
        }
    }
}

尝试此代码。