Swift - Get file size from url

后端 未结 5 1030
终归单人心
终归单人心 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: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)")
    }
    

提交回复
热议问题