【问题标题】:Downloading Firebase Storage Files Device Issue下载 Firebase 存储文件设备问题
【发布时间】:2017-03-08 04:00:45
【问题描述】:

我正在尝试从 Firebase 存储下载 Word 文档。在模拟器上,一切都按预期工作。然而在我的设备上,我收到以下错误:

Optional(Error Domain=FIRStorageErrorDomain Code=-13000 "发生未知错误,请检查服务器响应。" UserInfo={object=26 October 2016.docx, bucket=app.appspot.com, NSLocalizedDescription=未知错误发生,请检查服务器响应。,ResponseErrorDomain=NSCocoaErrorDomain, NSFilePath=/tmp/bulletin, NSUnderlyingError=0x1702590b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not allowed"}, ResponseErrorCode=513})

我一直在查看的其他帖子似乎没有给我一个有效的答案,我所知道的是文件权限存在问题,即使我使用的是推荐的目录 (tmp)。

这是下载文件的代码

 let Ref_Bulletin = Bulletin.referenceForURL("gs:/app.appspot.com/Bulletin/\(Today.stringFromDate(NSDate())).docx")

    // Create local filesystem URL
    let localURL: NSURL! = NSURL(string: "file:///tmp/today.docx")


        // Download to the local filesystem
        let downloadTask = Ref_Bulletin.writeToFile(localURL) { (URL, error) -> Void in
            if (error != nil) {
                print(error.debugDescription)
                // Uh-oh, an error occurred!
            } else {
                print("Working As Expected")
                self.Web_View.loadRequest(NSURLRequest(URL: localURL))
            }

那么是什么导致了这个问题,我该如何解决呢?

更新:

所以我尝试创建目录,但我被告知我没有权限,即使文档说我可以写入 tmp。

无法创建目录 Error Domain=NSCocoaErrorDomain Code=513 "您没有权限将文件 "today.docx" 保存在文件夹 "h" 中。" UserInfo={NSFilePath=/tmp/h/today.docx, NSUnderlyingError=0x1702498a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not allowed"}}

这是创建目录的代码:

 do {
        try NSFileManager.defaultManager().createDirectoryAtPath(localURL.path!, withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
        NSLog("Unable to create directory \(error.debugDescription)")
    }

【问题讨论】:

标签: swift download firebase-storage tmp


【解决方案1】:

我认为这里的问题是tmpDocuments 目录实际上并不存在于/tmp/Documents(例如,看起来/Documents 实际上是/User/Documents,实际上是/private/var/mobile/Documents,见:https://www.theiphonewiki.com/wiki/)

您需要确保根据系统认为这些目录的位置而不是字符串来创建文件 URL:

NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory()];
NSURL *fileURL = [[tmpDirURL URLByAppendingPathComponent:@"my_file"] URLByAppendingPathExtension:@"txt"];

NSDocumentDirectory 或类似。至于为什么这在模拟器上有效:我认为这是因为模拟器上的沙盒与真实设备的工作方式不同,并且/tmp 显然是您可以写入的有效位置(尽管可能不是那个你想写两个,当你尝试在真实设备上做这件事时,iOS 会发出嘶嘶声就证明了这一点)。

【讨论】:

  • 感谢 Mike,现在可以使用了。但是,我认为其他问题中没有提到这一点。
【解决方案2】:

我的情况是这样的;

我试图在存储引用上使用 firebase 的 write(toFile: myLocalURL) 方法,但 myLocalURL 是在文档目录中生成的;

FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
.appendingPathComponent("\(fileName).\(fileFormat)")

这就是问题所在,所以我需要先write(toFile: temporaryLocation) 到一个临时位置,然后移动到所需的目的地。

FileManager.default.temporaryDirectory
.appendingPathComponent("\(fileName).\(fileFormat)")

【讨论】:

    猜你喜欢
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多