0

Im trying to upload multiple image and video from library to url. evertying works fine except , response turns only one file. I choose two different image for experiment but response is only for last image in array. here is my upload func..

 func upload() {
    var mediaData = Data()
    var fname = ""
    var mimetype = ""
    let url = URL(string: "https://myurl.com/api/attachment/upload")
        let headers: HTTPHeaders = [
            "Authorization": "my Authorization Token",
            "Content-type": "multipart/form-data"
        ]

        Alamofire.upload(multipartFormData: { multipartFormData in
               for n in 0..<self.array.count{
                    mediaData = self.array[n].data
                    if self.array[n].type == "image"{
                        fname = "resim\(n).jpeg"
                        mimetype = "image/jpeg"
                    }else{
                        fname = "video\(n).mp4"
                        mimetype = "video/mp4"
                    }
                    multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)
                }


        },
          to: url!, method: .post, headers: headers) { (result) in
            switch result{
            case .success(let upload, _, _):
                upload.uploadProgress(closure: { (progress) in

                    print("uploding: \(progress.fractionCompleted)")
                })


                upload.responseJSON{ response in
                    print(response)
                    self.hud.dismiss()

                    if let err = response.error{
                        print(err)
                        return
                    }

                }
            case .failure(let error):
                print("Error in upload: \(error.localizedDescription)")

            }
        }
}

and this is what server returns as response.

SUCCESS: {
files =     (
            {
        "local_name" = BaN9x05lU8jNQPJ30RfbT4rqismLp7LE45sHezHvI;
        "real_name" = "resim1.jpeg";
    }
);
}

Any idea Where I made mistake?

0

2 Answers 2

1
check below code

var dataArray = [("name":"unicorn1","imageData":imgData1),("name":"unicorn2","imageData":imgData2)] Alamofire.upload( multipartFormData: { multipartFormData in

        for dat in dataArray {

           multipartFormData.append(dat.imageData, withName: dat.name)
        }

    },
    to: "https://httpbin.org/post",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your consideration but my problem is array is not static. It might be one or ten.
I edited the answer ,pls check it now if it helped then pls upvote
thank you for your concern but I found the solution.
0

I solved the problem changing "files[\(n)]" to code part of

 multipartFormData.append(mediaData, withName: "files", fileName: fname, mimeType:mimetype)

then problem solved.

func upload() {
var mediaData = Data()
var fname = ""
var mimetype = ""
let url = URL(string: "https://myurl.com/api/attachment/upload")
    let headers: HTTPHeaders = [
        "Authorization": "my Authorization Token",
        "Content-type": "multipart/form-data"
    ]

    Alamofire.upload(multipartFormData: { multipartFormData in
           for n in 0..<self.array.count{
                mediaData = self.array[n].data
                if self.array[n].type == "image"{
                    fname = "resim\(n).jpeg"
                    mimetype = "image/jpeg"
                }else{
                    fname = "video\(n).mp4"
                    mimetype = "video/mp4"
                }
                multipartFormData.append(mediaData, withName: "files[\(n)]", fileName: fname, mimeType:mimetype)
            }


    },
      to: url!, method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in

                print("uploding: \(progress.fractionCompleted)")
            })


            upload.responseJSON{ response in
                print(response)
                self.hud.dismiss()

                if let err = response.error{
                    print(err)
                    return
                }

            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")

        }
    }

}

Comments

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.