簡體   English   中英

如何使用 swift 從 iphone 設備上傳文件 Doc/pdf/excel 到后端?

[英]How to Upload file Doc/pdf/excel from iphone device into the backend using swift?

我需要從iphone中獲取可能是doc/pdf/excel文件的附件,然后發送到后端如果有人知道如何做請指導

讓我告訴你我做了什么 1.我一直在使用 UIDocumentPickerViewController

  1. 下面的function打開iCloud

func clickFunction(){

 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.這些是委托方法

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

當在此委托下選擇文檔時,您需要將其轉換為數據

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

並使用此上傳 function 將該文件發送到服務器

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)

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM