[英]Download and view document in ios from firebase storage
I have a firebase storage link of a document which I want to download in my ios device's Document directory and then view it from there using QuickLook Module. 我有一个Firebase文档存储链接,该链接要下载到ios设备的文档目录中,然后使用QuickLook模块从那里查看。 I am able to download document in iOS device but it is not saving with the name i am providing. 我可以在iOS设备上下载文档,但无法使用我提供的名称进行保存。 I want to create following directory structure and then need to save document in "doc" folder. 我想创建以下目录结构,然后将文档保存在“ doc”文件夹中。 Documents/chat/doc 文件/聊天/文件
following code is to create "/chat/doc" inside Iphone's Document directory. 以下代码是在Iphone的Document目录中创建“ / chat / doc”。 Where folderName
will contain "/chat/doc". 其中folderName
将包含“ / chat / doc”。
func createDocument(folderName: String ,completion: @escaping(Bool,String)->()){
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentsDirectory = path{
//
let docDirectoryPath = documentsDirectory.appending(folderName)
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: docDirectoryPath) {
do {
try fileManager.createDirectory(atPath: docDirectoryPath,
withIntermediateDirectories: true,
attributes: nil)
//return path on success
completion(true,docDirectoryPath)
return
} catch {
print("Error creating folder in documents dir: \(error.localizedDescription)")
completion(false,error.localizedDescription)
return
}
}
completion(true,docDirectoryPath)
}
}
after creating folder i want to download document and need to store in newly created folder in Document Directory. 创建文件夹后,我想下载文档,并且需要存储在文档目录中的新创建的文件夹中。
Following code will download document. 以下代码将下载文件。
func downloadDocument(){
createDocument(folderName: "/chat/doc") { (status, path) in
if let docURL = self.documentURL{
if status{
//folder is created now download document
let docDownloadRef = Storage.storage().reference(forURL: docURL)
//get documents metadata from firebase to get document name
docDownloadRef.getMetadata { metadata, error in
if let error = error {
// Uh-oh, an error occurred!
} else {
//get document name from metadata
if let fileName = metadata?.name{
//create file system url to store document
let docsurl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("chat/\(fileName)")
_ = docDownloadRef.write(toFile: myurl) { url, error in
if let error = error {
print(error.localizedDescription)
} else {
// Local file URL for document is returned
print("doc url \(url?.absoluteString)")
}
}
}
}
}
}else{
//folder is not created show document using online location
}
}
}
}
Ultimate goal is to achieve something like this "Documents/chat/doc/abc.doc"
but my code is not storing document using file name it stores like this "Documents/chat/doc" where doc is considered as a document not a folder. 但是我的代码不是使用文件名存储文档,而是像“文档/聊天/文档”这样存储文档,其中doc被视为文档而不是文件夹。
Please help to let me know what I am doing wrong. 请帮助让我知道我在做什么错。 Thank you. 谢谢。
When you are creating url
at below line, here you are missing the path for doc
folder. 在下一行创建url
时,此处缺少doc
文件夹的路径。
let myurl = docsurl.appendingPathComponent("chat/\(fileName)")
This will save to chat
folder. 这将保存到chat
文件夹。 You need to pass the same folder while creating ie folderName: "/chat/doc"
so it should be 您需要在创建时传递相同的文件夹,即folderName: "/chat/doc"
因此它应该是
let myurl = docsurl.appendingPathComponent("chat/doc/\(fileName)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.