Swift - Get file size from url

后端 未结 5 1015
终归单人心
终归单人心 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

    It's very easy with latest version of swift to calculate size of file using byte counter formatter:

    var fileSizeValue: UInt64 = 0

        do {
    
            let fileAttribute: [FileAttributeKey : Any] = try FileManager.default.attributesOfItem(atPath: url.path)
    
            if let fileNumberSize: NSNumber = fileAttribute[FileAttributeKey.size] as? NSNumber {
                fileSizeValue = UInt64(fileNumberSize)
    
                let byteCountFormatter: ByteCountFormatter = ByteCountFormatter()
                byteCountFormatter.countStyle = ByteCountFormatter.CountStyle.file
    
                byteCountFormatter.allowedUnits = ByteCountFormatter.Units.useBytes
                print(byteCountFormatter.string(fromByteCount: Int64(fileSizeValue)))
    
                byteCountFormatter.allowedUnits = ByteCountFormatter.Units.useKB
                print(byteCountFormatter.string(fromByteCount: Int64(fileSizeValue)))
    
                byteCountFormatter.allowedUnits = ByteCountFormatter.Units.useMB
                print(byteCountFormatter.string(fromByteCount: Int64(fileSizeValue)))
    
            }
    
        } catch {
            print(error.localizedDescription)
        }
    
    0 讨论(0)
  • 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
        }
    }
    
    0 讨论(0)
  • 2021-02-12 05:01

    First of all, in the file system you get the path of a URL with the path property.

    self.path = url.path
    

    But you don't need that at all. You can retrieve the file size from the URL directly:

    self.path = String(describing: self.file!) // url to string

    do {
        let resources = try url.resourceValues(forKeys:[.fileSizeKey])
        let fileSize = resources.fileSize!
        print ("\(fileSize)")
    } catch {
        print("Error: \(error)")
    }
    
    0 讨论(0)
  • 2021-02-12 05:01

    Swift 4.1 & 5

    func fileSize(forURL url: Any) -> Double {
            var fileURL: URL?
            var fileSize: Double = 0.0
            if (url is URL) || (url is String)
            {
                if (url is URL) {
                    fileURL = url as? URL
                }
                else {
                    fileURL = URL(fileURLWithPath: url as! String)
                }
                var fileSizeValue = 0.0
                try? fileSizeValue = (fileURL?.resourceValues(forKeys: [URLResourceKey.fileSizeKey]).allValues.first?.value as! Double?)!
                if fileSizeValue > 0.0 {
                    fileSize = (Double(fileSizeValue) / (1024 * 1024))
                }
            }
            return fileSize
        }
    
    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题