【问题标题】:cannot Uploading text file via Alamofire无法通过 Alamofire 上传文本文件
【发布时间】:2019-04-19 10:11:45
【问题描述】:

我正在尝试通过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")
        }

【问题讨论】:

  • 我找不到你的评论了,你是说 Alamofire 不能上传文件文本吗?你觉得我可以用图片做同样的测试吗?

标签: ios swift alamofire


【解决方案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)
            }
    })

【讨论】:

  • API_URL_HERE 只是 web url 上的一个文件夹,我还需要别的吗?我应该在图像数据上放什么?我正在使用我刚刚在项目中拖放的图像“image.jpg”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-07
  • 1970-01-01
  • 2016-06-07
  • 2020-02-10
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多