Swift - Get file size from url

后端 未结 5 1017
终归单人心
终归单人心 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 04:44

    extension URL {
        func fileSize() -> Double {
            var fileSize: Double = 0.0
            var fileSizeValue = 0.0
            try? fileSizeValue = (self.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! Double?)!
            if fileSizeValue > 0.0 {
                fileSize = (Double(fileSizeValue) / (1024 * 1024))
            }
            return fileSize
        }
    }
    

提交回复
热议问题