2017-05-05 4 views
5

私は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)") 
     } 

    } 

エラーメッセージが表示されます。このファイルは存在しません。ドキュメントピッカーがファイルを保存する場所と属性を取得する方法は次のとおりです。

+0

おそらくこれがあなたが探しているものです:http://stackoverflow.com/questions/19315533/how-to-find-size-of-a-file-before-downloading-it-in-ios -7 –

+0

ありがとう、リンクは私を助けた。 –

+0

iOS 11では –

答えて

11

まず、ファイルシステムでは、pathというプロパティを持つURLのパスを取得します。

self.path = url.path 

しかし、あなたはそのすべてでは必要ありません。あなたは直接URLからファイルサイズを取得することができます。

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

を歓迎していますが、エラーが発生しています。Error Domain = NSCocoaErrorDomain Code = 257「表示する権限がないためファイルを開くことができませんでした」 – Sneha

0

をそれはバイトカウンタフォーマッタを使用してファイルのサイズを計算するための迅速な最新のバージョンでは非常に簡単です:

の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) 
    } 
2

スイフト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 
} 
関連する問題