【问题标题】:How to get file name and file size from UIDocumentPickerViewController in iOS 14?如何从 iOS 14 中的 UIDocumentPickerViewController 获取文件名和文件大小?
【发布时间】:2021-07-26 03:39:53
【问题描述】:

我必须创建功能以上传一些 pdf 类型的文档,并且必须在上传之前显示名称和文件大小以及我从 UIDocumentPickerViewController 's delegate 选择的文件。

我怎样才能得到它的名字和文件大小?

【问题讨论】:

    标签: ios swift uidocument uidocumentpickerviewcontroller


    【解决方案1】:

    在您的UIDocumentPickerDelegate 中,您最终将实现documentPicker(_:didPickDocumentsAt:),这将为您提供URLs 的数组。您可以使用url.lastPathComponent 获取文件的名称。

    关于文件大小,一旦你从上面得到了相同的URL,你就可以查询文件大小(可能在这里列出的解决方案:Swift - Get file size from url):

    do {
      let attribute = try FileManager.default.attributesOfItem(atPath: url.path)
      if let size = attribute[FileAttributeKey.size] as? NSNumber {
        let sizeInMB = size.doubleValue / 1000000.0
      }
    } catch {
      print("Error: \(error)")
    }
    

    【讨论】:

    • 哦!太棒了,感谢您的帮助@jnpdx。我可以再问一个问题吗?对于 UIDocumentViewController,我已请求任何权限或打开任何功能?
    • @NawinP。我很确定,没有。
    • 非常感谢您的帮助:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2012-03-08
    • 2013-10-14
    • 1970-01-01
    相关资源
    最近更新 更多