本文介绍了从Firebase存储下载文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
正如我在文档中阅读的那样,我可以像这样访问Firebase存储中的单个URL:
As i read in the documentation i can access single url in firebase storage like this:
`// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")
// Fetch the download URL starsRef.downloadURL { url, error in
if let error = error {
// Handle any errors }
else {
// Get the download URL for 'images/stars.jpg'
} }`
但是,我那里有很多文件,那么如何跳过给出的直接路径,而是遍历给定目录中的所有文件?
However, i have many files there, so how can i skip giving direct path and instead iterate through all files in the given directory?
感谢提示.
推荐答案
DownloadURL一次只包含一个字符串.如果您希望将文件夹中的所有文件显示给像我这样的表格视图,请按以下步骤操作完整代码:
DownloadURL takes single string at a time. In case you want to show all the files inside a folder to a tableview like me, here is thefull code:
import UIKit import Firebase
我的第一个View Controller-
My very First View Controller-
class FolderList: UIViewController {
var folderList: [StorageReference]?
lazy var storage = Storage.storage()
@IBOutlet weak var tableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.storage.reference().child("TestFolder").listAll(completion: {
(result,error) in
print("result is \(result)")
self.folderList = result.items
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
} }
extension FolderList : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return folderList?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FolderListCell", for:
indexPath) as? FolderListCell else {return UITableViewCell()}
cell.itemName.text = folderList?[indexPath.row].name
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64.0
} }
extension FolderList : UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
guard let downloadVC = storyBoard.instantiateViewController(withIdentifier:
"DownloadedItemView") as? DownloadedItemView else {
return
}
downloadVC.storageRef = folderList?[indexPath.row]
self.navigationController?.pushViewController(downloadVC, animated: true)
}
}
每个单元格:
class FolderListCell: UITableViewCell {
@IBOutlet weak var itemName : UILabel!
}
这篇关于从Firebase存储下载文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!