2017-05-05 9 views
5

से फ़ाइल का आकार प्राप्त करें मैं किसी दस्तावेज़ के यूआरएल पथ प्राप्त करने के लिए दस्तावेज़पिकर का उपयोग कर रहा हूं और फिर डेटाबेस पर अपलोड किया गया हूं। मैं फ़ाइल (पीडीएफ, txt ..) चुन रहा हूं, अपलोड काम कर रहा है लेकिन मैं फ़ाइल के आकार को सीमित करना चाहता हूं।स्विफ्ट - यूआरएल

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

आपका स्वागत है ;-) –

उत्तर

11

सबसे पहले, फ़ाइल सिस्टम में आपको path संपत्ति के साथ एक यूआरएल का मार्ग मिलता है।

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

आईओएस 11 में, मुझे त्रुटि मिल रही है त्रुटि डोमेन = NSCocoaErrorDomain Code = 257 "फ़ाइल खोला नहीं जा सका क्योंकि आपको इसे देखने की अनुमति नहीं है।" – Sneha

0

यह बाइट काउंटर फ़ॉर्मेटर का उपयोग कर फ़ाइल के आकार की गणना करने के तेज के नवीनतम संस्करण के साथ बहुत आसान है:

वर 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 
} 
संबंधित मुद्दे