Как загрузить PDF-файл в FireBase с помощью iOS - PullRequest
0 голосов
/ 25 октября 2018

Могу ли я загрузить pdf файлы в firebase, используя Swift ?.Если это возможно, пожалуйста, поделитесь мне кодом.

Я использую код ниже

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 голосов
/ 25 октября 2018
// 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
        }
    }
}

Попробуйте этот код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...