无法通过Alamofire上传文本文件

时间:2018-11-16 08:40:09

标签: ios swift alamofire

我正在尝试通过Alamofire测试上传速度,将文件文本发送到服务器。尝试过thisthis,但无法使其正常工作,我应该为 myFile.txt 使用哪种数据?我需要在某个地方安装PHP脚本吗?

   let parameters = ["file_name": "myFile.txt"]
    Alamofire.upload(multipartFormData: { (multipartFormData) in

        //I cannot substitute this line of code

        //     multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg")

        for (key, value) in parameters {
            multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
        }
    }, to:"example.com")
    { (result) in
        switch result {
        case .success(let upload, _, _):

            upload.uploadProgress(closure: { (progress) in
                //Print progress
                print("in progress...")
            })

            upload.responseJSON { response in
                //print response.result
                print(response)
            }

        case .failure(let encodingError):
            //print encodingError.description
            print("error")
        }

1 个答案:

答案 0 :(得分:1)

   Alamofire.upload(multipartFormData: {

        multipartFormData in

        if IMAGE_DATA_HERE.count > 0 {

            multipartFormData.append((IMAGE_DATA_HERE), withName: "image", fileName: "file.png", mimeType: "image/jpeg")
        }
   }, to: API_URL_HERE, method:.post, headers:nil, encodingCompletion: {
        encodingResult in

        switch encodingResult {

            case .success(let upload, _, _):
                upload.uploadProgress { progress in

                    //For Upload Progress
                }

                upload.responseJSON { response in

                   //For JSON Response
                }

            case .failure(let encodingError):

                print(encodingError, logLevel: .DEBUG)
            }
    })