【问题标题】:FileManager IOS文件管理器 IOS
【发布时间】:2022-06-30 03:09:14
【问题描述】:

我是 swift/Xcode 的初学者。我已经在我的模拟器和我的 Iphone 中通过空投在 Document 中放置了一个 csv 文件。我试图找到它,我总是:文件不可用 我查看了我的 Iphone 文件在 Document 中 你有解决这个问题的想法吗? 除非我知道路径,否则是否有解决方案可以在我的 Iphone 或模拟器中的任何位置找到它? 非常感谢 最好的问候

***let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let url = NSURL(fileURLWithPath: path)
if let pathComponent = url.appendingPathComponent("players.csv") {
    let filePath = pathComponent.path
    let fileManager = FileManager.default
    if fileManager.fileExists(atPath: filePath) {
        print("FILE AVAILABLE")
    } else {
        print("FILE NOT AVAILABLE")
    }
} else {
    print("FILE PATH NOT AVAILABLE")
}***

【问题讨论】:

  • .documentDirectory您的 应用的 Documents 目录。如果您想访问用户的文件,您需要使用 UIDocumentPickerViewController 之类的东西,但如果您是初学者,这可能是一个更高级的主题。

标签: ios swift


【解决方案1】:

【讨论】:

  • 非常感谢您的回答。事实上,文件“players.csv”不在 documentDirectory 中,而是在我的 Iphone 的云驱动器中。这就是我在文件管理器中找不到的原因。但我不知道是否可以通过在云驱动器中搜索文件管理器找到它。
【解决方案2】:
public static bool CheckSize(IFormFile file, int kb)
    {
        if (file.Length > kb / 1024) return false;
        return true;
    }
    public static bool CheckType(IFormFile file, string path)
    {
       if(file.ContentType.Contains(path)) return true;
        return false;
    }
    public static string SaveImg(IFormFile file, string savePath)
    {
        string fileName = Guid.NewGuid().ToString() + file.FileName;
        string fullPath = Path.Combine(savePath, fileName);
        using(FileStream fs = new FileStream(fullPath, FileMode.Create))
        {
            file.CopyTo(fs);
        }
        return fileName;
    }

    public static void DeleteImg(string path)
    {
        if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多