0

如何从 UIDocumentPickerController 获取文件 url,以便稍后上传到我的服务器?发送文件作为实现的电报。现在只能从 icloud 下载文件并稍后发送。但是我想学习如何在电报中实现?

这是我现在的代码:

func openPicker() {
    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["public.data"], inMode: UIDocumentPickerMode.Open)
    documentPicker.delegate = self
    documentPicker.modalPresentationStyle = UIModalPresentationStyle.FullScreen
    self.presentViewController(documentPicker, animated: true, completion: nil)
}

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    if controller.documentPickerMode == UIDocumentPickerMode.Open {
        let fileManager = NSFileManager.defaultManager()
        print(fileManager.fileExistsAtPath(url.path!))
        let data = NSData(contentsOfFile: url.path!)
        let document = UIDocument(fileURL: url)
    }
}
4

1 回答 1

0

这对我有用,我能够获取文件的 url 并从中读取内容,而无需下载文件

let documentPickerController = UIDocumentPickerViewController(documentTypes: [ICLOUD_DOCUMENT_SEARCH_TYPE], inMode: UIDocumentPickerMode.Import)
        documentPickerController.delegate = self
        self.presentViewController(documentPickerController, animated: true, completion: nil)

func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
    print(url)
}

然后,您可以从 url 使用 NSFileManager 读取文件的内容。

于 2017-03-29T10:21:44.153 回答