0

我已经在 Flutter 中下载了一个文件并使用该path_provider包,将它放在我从中获取的目录中getTemporaryDirectory()。然后使用MethodChannels调用一个方法,我调用的是下面的方法:

private func saveDocument(_ filePath: String, _ result: @escaping FlutterResult) {
        let fileManager = FileManager.default
        print("File exists: \(fileManager.fileExists(atPath: filePath))") // Prints -> File exists: true
        do {
            let documentsDir = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let documentsDirString = documentsDir.absoluteString
            
            print("Documents dir exists: \(fileManager.fileExists(atPath: documentsDirString))") // Prints -> Documents dir exists: false
            
            if (!fileManager.fileExists(atPath: documentsDirString)) {
                try fileManager.createDirectory(at: documentsDir, withIntermediateDirectories: true, attributes: nil)
            }
            
            let fileName = (filePath as NSString).lastPathComponent
            let newFilePath = "\(documentsDir.appendingPathComponent(fileName))"
            try fileManager.copyItem(at: URL(fileURLWithPath: filePath), to: URL(fileURLWithPath: newFilePath))
            result(true)
        } catch {
            print(error)
            result(false)
        }
    }

我在控制台中收到以下错误:

File path: /Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf
File exists: true
Documents dir exists: false
2021-04-26 10:01:51.693311+0530 Runner[9709:78939] open on /file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Documents/filename.pdf: No such file or directory
Error Domain=NSCocoaErrorDomain Code=4 "The file “filename.pdf” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Documents/filename.pdf, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/CE787CF1-1A9E-44C0-90FA-48928A3CE5F9/Library/Caches/filename.pdf, NSUnderlyingError=0x6000012baa30 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}

我已经阅读了文档并在网上搜索了大约 2 天,但我找不到解决方案。

我想要达到的目标

将文件移动到用户的文件管理器中,以便他们可以从“文件”应用程序查看、修改、共享等文件

我也试过在物理设备上运行它,但没有运气

我试过的:

  • 删除try fileManager.createDirectory()if 语句。以下是删除此日志的日志:
File path: /Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf
File exists: true
Documents dir exists: false
2021-04-26 10:00:11.558244+0530 Runner[9338:76668] open on /file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Documents/filename.pdf: No such file or directory
Error Domain=NSCocoaErrorDomain Code=4 "The file “filename.pdf” doesn’t exist." UserInfo={NSSourceFilePathErrorKey=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf, NSUserStringVariant=(
    Copy
), NSDestinationFilePath=/file:/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Documents/filename.pdf, NSFilePath=/Users/user/Library/Developer/CoreSimulator/Devices/A55EF2F7-FD8A-469C-A835-1A6282066F50/data/Containers/Data/Application/A00C3B10-8E12-44F1-B8F8-B53692CD4B56/Library/Caches/filename.pdf, NSUnderlyingError=0x600003682130 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
4

1 回答 1

0

我发现您必须共享文件才能将其保存到Files。我将使用该share软件包来执行此操作。

于 2021-04-26T05:45:58.310 回答