繁体   English   中英

如何从 iOS 14 中的 UIDocumentPickerViewController 获取文件名和文件大小?

[英]How to get file name and file size from UIDocumentPickerViewController in iOS 14?

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

如何获取其名称和文件大小?

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

关于文件大小,一旦你有相同的URL从上面,你可以查询文件大小(可能在这里列出的解决方案: Swift - 从 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)")
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM