Добавить данные в тело сообщения и загрузить в API - PullRequest
0 голосов
/ 09 декабря 2018

Я вызываю один API с передачей некоторого параметра, включая получение некоторого файла из icloud, такого как pdf, doc, docx и вызов API.

Теперь я выбираю какой-то файл из icloud, и мне нужно перейти квызов API.Проблема в том, что мой выбранный файл (pdf или doc) не преобразуется в nsdata, а байты идут как 0. Так что он не зависит от параметров моего тела. Помогите мне, где я делаю неправильно

Мой код:

func uploadthefileToserver(){

    if let url = URL(string: "https://www.exampleurl/api"){
        var request = URLRequest(url: url)
        let boundary:String = "Boundary-\(UUID().uuidString)"
        // let request = NSMutableURLRequest(url:myUrl! as URL);
        request.httpMethod = "POST"
        request.timeoutInterval = 10
        request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

        let postJobData:[String:Any] = ["UserId":"107","name":"hardcodevalue"]
        var dataFile: Data = Data()
        print(fullDestPath)   ///Users/sathish/Library/Developer/CoreSimulator/Devices/4464E7A8-0F38-4802-B645-19721D251054/data/Containers/Data/Application/714B1B8E-5872-42B9-B963-B0C51C9403D7/Documents/NewFileiCloud/iOS.DOCX"
        do{
            dataFile = try NSData.init(contentsOf: URL(fileURLWithPath: fullDestPath, isDirectory: true)) as Data
            print(dataFile)
        }catch{
            print(error)
        }
        if(dataFile==nil)  { return; }
        print(dataFile) //0 bytes
        request.httpBody = createBodyWithParameters(parameters: postJobData, filePathKey: "Resume", FileData: dataFile as NSData , boundary: boundary) as Data
        print(postJobData)
        print(dataFile)

        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in


            if error != nil {
                print("error=\(error)")
                return
            }else if let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue){
                print("****** response data = \(responseString)")
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary
                    print(json)

                    let status = json!["Success"] as! String
                    let errMessage = json!["Message"] as? String
                    DispatchQueue.main.async() {
                        if status == "1"{
                            print(errMessage)

                        }else{
                            print(errMessage)
                        }
                    }

                }catch{
                    print(error)
                }
            }

        }; task.resume()
    }
}

Не уверен, где я делаю не так.Я поставил некоторые функции печати для референции.

Основные части:

dataFile = try NSData.init(contentsOf: URL(fileURLWithPath:
fullDestPath, isDirectory: true)) as Data

request.httpBody = createBodyWithParameters(parameters: postJobData,
filePathKey: "Resume", FileData: dataFile as NSData , boundary:
boundary) as Data

Спасибо

Обновление:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {

        print("url = \(urls)")
        filePathUrl = urls
        print(filePathUrl)
        for urll in filePathUrl{
            filepath = filePathUrl[0] as! URL
            print(filepath)
            filePathString = filepath.path
            urlstr = NSURL(fileURLWithPath: filePathString).lastPathComponent!
            print(urlstr)
            // Data object to fetch weather data
            do {
                let weatherData = try NSData(contentsOf: filepath, options: NSData.ReadingOptions())
                print(weatherData)

            } catch {
                print(error)
            }

        }
        let destPath:NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
        let fileManager = FileManager.default
        print(destPath, "\n")
        documentDir = destPath[0] as? NSString
        let filePath = documentDir?.appendingPathComponent("NewFileiCloud") as! NSString

      //  if fileManager.fileExists(atPath: filePath as String){

            do {
               // try fileManager.createDirectory(atPath: filePath as String, withIntermediateDirectories: false, attributes: nil)
                fullDestPath = filePath.appendingPathComponent(urlstr)
                print(fullDestPath!) ///Users/sathish/Library/Developer/CoreSimulator/Devices/4464E7A8-0F38-4802-B645-19721D251054/data/Containers/Data/Application/E41634D7-681A-4C09-B3EF-5782CECCF4B0/Documents/NewFileiCloud/filke.pdf
                do{
                    try fileManager.copyItem(atPath: filePathString!, toPath: fullDestPath)
                }catch{
                    print("\n")
                    print(error)
                }
            }catch{
                print(error)
            }
       // }

      // ------- This is the path of the application stored filepath -------------- //

        filePathLabel.text = fullDestPath

// ------------------- ---------------------------------//
// Read a file content
        //     fileContent = fileManager.contents(atPath: fullDestPath as String ) as! NSData
        //       print(fileContent)

        uploadthefileToserver()
    }

 func createBodyWithParameters(parameters: [String: Any]?, filePathKey: String?, FileData: NSData, boundary: String) -> NSData {

        let body = NSMutableData();

        if parameters != nil {
            for (key, value) in parameters! {
                body.appendString(string:"--\(boundary)\r\n")
                body.appendString(string: "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
                body.appendString(string: "\(value)\r\n")
            }
        }

        return body

        let filename = fullDestPath
        let mimetype = "pdf/docx/text"

        body.appendString(string: "--\(boundary)\r\n")
        body.appendString(string: "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n")
        body.appendString(string: "Content-Type: \(mimetype)\r\n\r\n")
        body.append(FileData as Data)
        body.appendString(string: "\r\n")
        body.appendString(string: "--\(boundary)--\r\n")

        return body
    }

1 Ответ

0 голосов
/ 09 декабря 2018

Вы пытаетесь сделать,

  1. Конвертировать PDFURL в данные

  2. Конвертировать данные в .PDF

  3. Сохранение этого .PDF в Doc.Dir.

  4. Извлечение этого .PDF из Doc.Dir и передача на сервер.

Для выполнения вышеуказанной задачи вы должны создать папку NewFileiCloud в Doc.Dir.Затем преобразуйте pdfUrl в data и запишите эти данные в файл .pdf, а затем получите этот путь .pdf из doc.dir и преобразуйте его в Data и передайте в server.

Я сделал образец для вас.Это вас удовлетворит.

override func viewDidAppear(_ animated: Bool) {

    // I have did sample for you by taking .pdf from bundle.
    if let pathPDF = Bundle.main.path(forResource: "sample", ofType: "pdf") {

        let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
        let documentDirectoryPath:String = path[0]
        let fileManager = FileManager()
        var destinationURLForFile = URL(fileURLWithPath: documentDirectoryPath.appending("/NewFileiCloud"))
        do {
            //You have to create directory with above name.
            try fileManager.createDirectory(at: destinationURLForFile, withIntermediateDirectories: true, attributes: nil)
            destinationURLForFile.appendPathComponent("reader.pdf")

            //YOUR PDF URL [pathPDF [my bundle path, you have to give your URL]] to DATA
            let pdfData = try Data(contentsOf: URL(fileURLWithPath: pathPDF))  

            // WRITE ITS CONTENT to Doc.Dir.
            try pdfData.write(to: destinationURLForFile, options:.atomic)

            //ASSIGN PATH TO GLOBAL URL VARIABLE
            fullPAth = destinationURLForFile


            print("conclude     ", destinationURLForFile)

            uploadToServer()

        }
        catch(let error){
            print(error)
        }

    }
}


func uploadToServer() {

    .....

    do {

        // Here you can get PDF contents as Data.
        // With this Data, you can pass to Server Side.
        let pdfPOSTData = try Data(contentsOf: fullPAth!)
    }
    catch let e{
        print("Catch_Not_worlk    ",  e)
    }

    ......
}
...