将文件转换为String然后在swift 3中上传?

时间:2017-07-22 08:12:22

标签: ios string post file-upload swift3

我的应用程序中有一个迷你文件管理器,当用户使用iTunes将文件复制到应用程序时检测应用程序中的文件 - 文件共享 - 我使用此方法将文件转换为字符串

 let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
            print(directoryContents)

            // if you want to filter the directory contents you can do like this:
            let pdfFiles = directoryContents.filter{ $0.pathExtension == "pdf" }
            print("pdf urls:",pdfFiles)
            filesurl = pdfFiles
            print(pdfFiles.count)
            filesCount = pdfFiles.count
            let pdfFileNames = pdfFiles.map{ $0.deletingPathExtension().lastPathComponent }
            print("pdf list:", pdfFileNames)
            filestext = pdfFileNames

        } catch let error as NSError {
            print(error.localizedDescription)
        }

  var names = String()
    let rows = fileTableView.indexPathsForSelectedRows.map{$0.map{$0.row}}
    print(rows!)

        for i in rows! {

        print(filesurl[i])
        print(filestext[i])
        names.append("\(filestext[i])")
            fileManagerViewController.filesForUpload.append(filesurl[i])



    }

        let urlString = "\(fileManagerViewController.filesForUpload)"
        let pathURL = URL(string: "\(fileManagerViewController.filesForUpload)")!
        print("the url = " + pathURL.path)

然后使用此方法上传

var requestF = URLRequest(url: URL(string: "http://example.com/api/file?api_token=\(EmailSignInViewController.api_token)")!)
        print(requestF)
        requestF.httpMethod = "POST"
        let postStringF = "myfile=\(fileManagerViewController.filesForUpload)"



          print(postStringF)

        requestF.httpBody = postStringF.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: requestF) { data, response, error in
            guard let data = data, error == nil else {

                                    print("error=\(error)")

                DispatchQueue.main.async {



                    let alertController = UIAlertController(title: "Error", message: "Error", preferredStyle: UIAlertControllerStyle.alert)

                    let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default)
                    {

                        (result : UIAlertAction) -> Void in

                        self.viewDidLoad()
                        self.viewWillAppear(true)

                    }
                    alertController.addAction(okAction)
                    self.present(alertController, animated: true, completion: nil)
                    alertController.view.tintColor = UIColor.red
                    alertController.view.backgroundColor = UIColor.red
                    alertController.view.layer.cornerRadius = 0.1 * alertController.view.bounds.size.width

                }

                return
            }

            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 500 {           // check for http errors


                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                //                   print("response = \(response)")
            }

            let responseString = String(data: data, encoding: .utf8)


            print(responseString!)
            print(responseString!)

        }

        task.resume()

并且在响应字符串行中我将得到此致命错误

2017-07-22 12:31:56.911679+0430 Tipo[718:251338] [Common] _BSMachError: port a807; (os/kern) invalid capability (0x14) "Unable to insert COPY_SEND"
2017-07-22 12:31:56.913507+0430 Tipo[718:251338] [Common] _BSMachError: port a807; (os/kern) invalid name (0xf) "Unable to deallocate send right"
responseString = Optional("118")
fatal error: Can't form Range with upperBound < lowerBound
2017-07-22 12:32:04.669800+0430 Tipo[718:251338] fatal error: Can't form Range with upperBound < lowerBound

0 个答案:

没有答案