ios - 如何使用 swift 将文件 Doc/pdf/excel 从 iphone 设备上传到后端?

标签 ios swift iphone icloud

我需要从 iPhone 获取可能是 doc/pdf/excel 文件的附件,然后发送到后端
请指导是否有人知道如何执行此操作
让我告诉你我还做了什么
1.我一直在使用 UIDocumentPickerViewController

  • 下面的函数打开iCloud

  • 函数点击函数(){
     let options = [kUTTypePDF as String, kUTTypeZipArchive  as String, kUTTypePNG as String, kUTTypeJPEG as String, kUTTypeText  as String, kUTTypePlainText as String]
    
         let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: options, in: .import)
    
        documentPicker.delegate = self
    
        documentPicker.modalPresentationStyle = .formSheet
    
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    3.这些是委托(delegate)方法
    公共(public) func documentPicker(_ Controller : UIDocumentPickerViewController,
    didPickDocumentsAt urls: [URL]) {
        guard let myURL = urls.first else {
            return
        }
    
        print("import result : \(myURL)")
    }
    
    
    
    public func documentMenu(_ documentMenu:UIDocumentMenuViewController, 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)
    }
    
    从上面的代码中,我在从 icloud 中选择任何文件后得到了 url,我有点困惑我应该用那个 url 做什么
    像:- file:///private/var/mobile/Containers/Data/Application/8F334336-525D-46F7-BF60-79FC6A45601A/tmp/com.Empro.WorkToday-Inbox/AbhishekSharma-MCA-1.pdf

    最佳答案

    选择文件后,您必须将其上传到 Moya,这是一个非常好的网络层,可以将图像/文档等文件上传到服务器

     let keyValue = "doc[0]"
       
    var multipartData = [MultipartFormData]()
    
    
    
    if(docTypes == .isTypePDF)
        {
            multipartData.append(Moya.MultipartFormData(provider: .data(documentData!), name: keyValue, fileName: "file.pdf", mimeType: "application/pdf"))
        }
        else
        {
            multipartData.append(Moya.MultipartFormData(provider: .data(documentData!), name: keyValue, fileName: "file.doc", mimeType: "application/doc"))
        }
    
    当在此委托(delegate)下选择文档时,您需要将其转换为数据
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
            
            if(urls.count > 0)
            {
                do
                {
                    let imageExtensions = ["pdf", "rtf", "docx", "doc"]
    
                    for i in 0..<urls.count
                    {
                        let urlvalue = urls[i]
                       
                        let pathExtention = urlvalue.pathExtension
                        if imageExtensions.contains(pathExtention)
                        {
                            print("Image URL: \(urlvalue)")
                            
                            if(pathExtention == "pdf")
                            {
                                self.doctype = .isTypePDF
                            }
                            else
                            {
                                self.doctype = .isTypeDoc
                            }
                            
                            // Do something with it
                        }else
                        {
                           print("Movie URL: \(urlvalue)")
                        }
                        
                        let fileName = urlvalue.lastPathComponent
    
                        let imgData1 = try Data.init(contentsOf: urlvalue)
                        print("testing")
                        
                        self.documentData = imgData1
                        
                        self.uploadDocumentName = fileName
                        self.tableView.reloadData()
                       // multipartData.append(Moya.MultipartFormData(provider: .data(imgData1), name: "audio[0]", fileName: "file.mp4", mimeType: "audio/mpeg"))
                    }
                }
                catch {
                    print("asdfasfd")
                }
            }
            
        }
    
    并使用此上传功能将该文件发送到服务器
    func updatePostData(formdata: [MultipartFormData], controller: UIViewController)
    {
        provider.request( .UpdatePost(formdata) , callbackQueue: DispatchQueue.main, progress: { (progress) in
            
            print(progress.progress)
    
        }) { (result) in
             
            switch result{
            case .success(let response):
                
                do {
                  loadLoader(isLoading: false)
                  
                  let responseResult = try response.map(BaseCordableResponseResult<Any>.self)
                  print(responseResult)
                  
    //              self.delegateObject?.reloadData()
    
                    NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
    
                   controller.dismiss(animated: true)
                                     
                } catch {
                  
                   controller.dismiss(animated: true)
    
                }
    
            case .failure(let error):
                checkLogoutUser(error: error, viewController: controller)
    
            }
        }
    }
    

    关于ios - 如何使用 swift 将文件 Doc/pdf/excel 从 iphone 设备上传到后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63983424/

    相关文章:

    iphone - zxing连续扫描-iphone

    iphone - 在提交到 App Store 之前确定 iOS 应用程序的文件大小

    iphone - 将 NSData 转换为 int

    swift - 我怎么知道要在 Swift 中抛出的核心函数中捕获哪些异常

    ios - Swift 元类型不一致

    ios - 在 nib 的 View 加载并显示在屏幕上后立即调用方法

    ios - 如何将 iOS 数据发送到 Azure 移动服务数据库?

    ios - 如何禁用选项卡栏项目以及如何更改禁用项目的颜色和透明度

    iphone - 通知发生后 UILocalNotification 显示 View ?

    ios - 键盘在 ios7 (Xcode 6) 中无法正常工作