【问题标题】:How to upload document using documentpicker in swift如何在 swift 中使用 documentpicker 上传文档
【发布时间】:2021-07-01 17:20:30
【问题描述】:

我正在尝试使用 documentpicker 上传文档。我可以从我的手机文档文件夹中浏览文档,但选择后什么也没有发生。我想获取该文档(例如(sample.text 或 sample.pdf)并将其上传到我的数据库服务器。

这是我的代码:

@IBAction func bio_choose_document_btn(_ sender: Any) 
   {

        let importDocument = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText), String(kUTTypeJPEG), String(kUTTypePNG)], in: .import)
        importDocument.delegate = self
        importDocument.allowsMultipleSelection = true
        importDocument.modalPresentationStyle = .formSheet
        self.present(importDocument, animated: true, completion: nil)
    }
    
    public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        guard let filepathurl = urls.first
            else {
            return
        }
        let fileName = filepathurl.lastPathComponent
        bio_no_file_choosen_lbl.text = fileName
        
        doc_webview.load(URLRequest(url: filepathurl))
        //let items = TableViewItem()
        //items?.documentname = self.bio_doc_view.text
        //self.TableView_item.append(items!)
        print("import result : \(filepathurl)")
        self.bio_documentview_tvc.reloadData()
    }
    
          

    public func documentMenu(_ documentMenu:UIDocumentPickerViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }


    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        print("view was cancelled")
        dismiss(animated: true, completion: nil)
    }

我正在使用 webview,但它会将整个文档作为视图打开。我不想打开它。我想将文档作为文件夹检索。

任何帮助将不胜感激。
谢谢!
鲁图帕纳熊猫

【问题讨论】:

    标签: swift


    【解决方案1】:

    通常是获取本地文件的文件路径(URL),然后通过文件路径(URL)上传文件。

    AFN/Alamofire 邮政

      //...
      do{
          try formData.appendPart(withFileURL: filepathurl, name: "file")
      } catch {
          DDLog("error:\(error.localizedDescription)")
      }
      //"file" is a field that is agreed with a server colleague
      //...
    
    

    github

    【讨论】:

    • 如何在视图控制器中打开选中的文件?我可以浏览,但是当我点击打开时,什么也没有发生。我想在我的表格视图单元格中检索该文件。
    • UIDocumentPickerViewContrller 只是一个文件选择器,如果要读取需要将 QLPreviewController 添加到当前控制器视图中,并处理代理方法。不好说清楚,你看链接demo,我之前实现过,包括文件选择、读取、下载、上传功能。@RutuparnaPanda
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多