-1

I'm trying to test upload speed via Alamofire, sending a file text to a server. tried this and this but cannot make it work, which kind of data should I use for myFile.txt? do I need a php script somewhere?

   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
  • I cannot find your comment anymore, did you say Alamofire cannot upload file text? do you think I could do the same test by using image? Commented Nov 16, 2018 at 9:41

1 Answer 1

2
   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)
            }
    })
Sign up to request clarification or add additional context in comments.

1 Comment

API_URL_HERE is simply a folder on web url o do I need something else? and what should I put on image data? I'm using an image "image.jpg" I just dragged and dropped in the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.