简体   繁体   中英

Swift data write to Document directory maintaining directory structure

I am downloading file from firebase. let say the request url is following

social-cam-storage/albm-72/owner-2/1484043313786.jpeg

i can download the file using the following code

func downloadFile(url : String) {
        let storageR = FIRStorage.storage().reference(withPath: url)
        let maxSize : Int64 = 3 * 1024 * 1024  // 3MB

        storageR.data(withMaxSize: maxSize) { (data, error) in
            if error != nil {
                print(error.debugDescription)
                return
            }

            print(data!)
        }

    }

Now i need to store this data maintaining the directory structure of the url

I have tried

let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
print(FileManager.default.createFile(atPath: "\(documentsURL.absoluteString)/\(url)", contents: data!, attributes: nil))

but i am getting false

so how to fix this or is there any other way to save??

Have you tried something like this? :

If you have the exact path already as a string:

try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])

If you need the path there are a few methods:

func saveFile() {
    let filePath = getDocumentsURL().absoluteString.appending(path)
    try? data.write(to: URL(fileURLWithPath: filePath), options: [.atomic])
}

func getDocumentsURL() -> URL {
    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    return documentsURL
}

You could also just try saving the filename, and then loading later when you need it:

func fileInDocumentsDirectory(_ filename: String) -> String {
    let fileURL = getDocumentsURL().appendingPathComponent(filename)
    return fileURL.path
}

// To save file
func saveFile(data: Data) {
    let fileName:String = "createUniqueFileName"
    let filePath = fileInDocumentsDirectory(fileName)
    saveData(data, filePath)
}

// To load file with saved file name
func loadFile(fileName: String) {

    if let loadedData = loadData(fileName) {
         // Handle data however you wish
    }

}

func saveData(_ data: Data, path: String ) {

    try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])

}

func loadData(_ path: String) -> Data? {

    let data:Data? = try? Data(contentsOf: URL(fileURLWithPath: path))

    return data

}

Have you tried using the built in " download to file " API in Firebase Storage?

// Create a reference to the file you want to download
let fileURL = storage.reference(withPath: url)

// Create local filesystem URL
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = ... 

// Download to the local filesystem
let downloadTask = islandRef.write(toFile: fileURL) { url, error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // Local file URL is returned
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM