【问题标题】:How to upload pdf file into firebase by using iOS如何使用 iOS 将 pdf 文件上传到 firebase
【发布时间】:2019-03-29 15:53:21
【问题描述】:

我可以使用Swiftpdf 文件上传到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")
}

提前致谢

【问题讨论】:

标签: ios objective-c swift firebase-storage


【解决方案1】:
// 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
        }
    }
}

试试这个代码。

【讨论】:

  • @Parameswar Reddy 我已执行此代码并更新了我的答案,请检查此
猜你喜欢
  • 2019-02-10
  • 2020-05-09
  • 2021-11-20
  • 2016-10-02
  • 2021-04-03
  • 2018-09-17
  • 2011-12-08
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多