gpt4 book ai didi

swift - Alamofire 没有将文件保存到磁盘

转载 作者:搜寻专家 更新时间:2023-10-31 22:49:23 26 4
gpt4 key购买 nike

我在 S3 上有一个视频文件,我正试图将其保存到磁盘。但是,如果文件已经存在于磁盘上,我想覆盖它。我写了这个函数来下载文件,但它从不保存文件。我可以看到进度百分比在增加。但是,如何访问生成的文件并将其保存到磁盘?

    var finalPath: NSURL?

Alamofire.download(.GET, s3Url) { temporaryURL, response in
let fileManager = NSFileManager.defaultManager()
if let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as? NSURL {
let pathComponent = response.suggestedFilename
finalPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
println(finalPath)
//remove the file if it exists
if fileManager.fileExistsAtPath(finalPath!.absoluteString!) {
println("file exists on disk, removing..")
fileManager.removeItemAtPath(finalPath!.absoluteString!, error: nil)
}
return finalPath!
}

return temporaryURL
}
.validate()

.progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in
let progress = (Double(totalBytesRead) / Double(totalBytesExpectedToRead)) * 100
println(String(format: "%.2f", progress))
}

.response { request, response, data, error in
println(request)
println(response)

if let mediaData = data {
println("saving file to disk")
mediaData.writeToURL(finalPath!, atomically: true)
}
}

通常我会使用文档中提供的示例,但如果文件已经存在,它会失败。即:

let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory, domain: .UserDomainMask)

那么,我该如何下载文件,如果存在就覆盖它,并将文件写入我的 coreData 数据库的路径记录下来?

最佳答案

您需要先删除该文件。 Alamofire 仅尝试将文件从临时位置移动到您在 destination 闭包中提供的最终位置。

关于swift - Alamofire 没有将文件保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931195/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com