私はdocumentPickerを使用して任意のドキュメントのURLパスを取得し、データベースにアップロードしています。私はファイル(pdf、txt ..)を選択しています。アップロードは機能していますが、ファイルのサイズを制限したいのですが。Swift - URLからファイルサイズを取得
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)")
}
}
エラーメッセージが表示されます。このファイルは存在しません。ドキュメントピッカーがファイルを保存する場所と属性を取得する方法は次のとおりです。
おそらくこれがあなたが探しているものです:http://stackoverflow.com/questions/19315533/how-to-find-size-of-a-file-before-downloading-it-in-ios -7 –
ありがとう、リンクは私を助けた。 –
iOS 11では –