Swift - Get file size from url

后端 未结 5 1022
终归单人心
终归单人心 2021-02-12 03:58

i am using documentPicker to get url path of any document and then uploaded to the database. I am choosing file (pdf, txt ..) , the upload is working but i want to limit the siz

5条回答
  •  南笙
    南笙 (楼主)
    2021-02-12 05:04

    Swift 4:

    func sizePerMB(url: URL?) -> Double {
        guard let filePath = url?.path else {
            return 0.0
        }
        do {
            let attribute = try FileManager.default.attributesOfItem(atPath: filePath)
            if let size = attribute[FileAttributeKey.size] as? NSNumber {
                return size.doubleValue / 1000000.0
            }
        } catch {
            print("Error: \(error)")
        }
        return 0.0
    }
    

提交回复
热议问题