Swift - get file size from url

I use documentPicker to get the URL of any document and then uploaded to the database. I select a file (pdf, txt ..), the download works, but I want to limit the file size.

 public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

        self.file = url //url
        self.path = String(describing: self.file!) // url to string
        self.upload = true //set upload to true
        self.attachBtn.setImage(UIImage(named: "attachFilled"), for: .normal)//set image
        self.attachBtn.tintColor = UIColor.black //set color tint
        sendbtn.tintColor = UIColor.white //


        do
        {
            let fileDictionary = try FileManager.default.attributesOfItem(atPath: self.path!)
            let fileSize = fileDictionary[FileAttributeKey.size]
            print ("\(fileSize)")
        } 
        catch{
            print("Error: \(error)")
        }

    }

I get an error, this file does not exist, where the document collector saves the file and its attributes.

+6
source share
3 answers

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

self.path = url.path

But you do not need it at all. You can directly get the file size from the URL:

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

do {
    let resources = try url.resourceValues(forKeys:[.fileSizeKey])
    let fileSize = resources.fileSize!
    print ("\(fileSize)")
} catch {
    print("Error: \(error)")
}
+11
source

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
}
+3
source

It is very simple with the latest version of quick file size calculation by formatting the byte counter:

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)
    }
+1
source

Source: https://habr.com/ru/post/1017009/


All Articles