asked    Roderick     2018-10-22       ios       76 view        2 Answers

[SOLVED] Cant upload multiple files from array with Alamofire

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?

  2 Answers  

        answered    Ogden     2018-10-22      

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)")

        }
    }

}



        answered    Dinah     2018-10-22      

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)
        }
    }
)




Your Answer





 2018-10-22         Miles

zoom image on mouse scroll in angular4?

I have a piece of code which will zoomin/out on mouse scroll as shown below<!DOCTYPE html><html><body><script src='https://www.cssscript.com/demo/image-zoom-pan-mouse-interactions-zoomnpan/zoomnpan.js'></script><style>#demo { width: 460px; height: 250px; background-image: url(https://unsplash.it/3000/2000/?random); background-repeat: no-repeat; background-position: 50% 50%;}</style><div id="demo"></div><script>var p;function enable(){ p = new ZoomNPan(demo); p.minScale = 100;}enable();</script></bod...
 javascript                     2 answers                     51 view
 2018-10-22         Hamiltion

Anchor link is not working from JSON string in ionic 3 and angular 4 : ur "<a (click)='openActionPage()'>terms and conditions</a>"

Am using this string in JSON file.In order to proceed, you need to agree with our "terms and conditions" and adding for accessing in frontend using <message *ngFor="let message of messages" [innerHTML]="message.content"> {{message.content}}</message>but the link openActionPage() is not opening in front end. Can anyone please suggest how me to solve this ? Thank you in advance so what you are trying to do here is to add dynamically some html code as innerHTML with anchor tag (a) and event binding to a method inside your ts file - this won't really work like...
 angular                     1 answers                     55 view
 2018-10-22         Brandon

Patching Items Doesn't Update Validity in Angular

I've a problem because after i submit the form, even though there's a value, the "field is required" doesn't disappear. It supposed to disappear. Is there something wrong with my validity? Please see this link See this link TSpatchValues(id, i) {let x = (<FormArray>this.addForm.controls['rows']).at(i);const selectedIngredient = this.ingredients.find(y => y.id == id);x.patchValue({ unit_price: selectedIngredient.price});} In these cases, you have to trigger a validity check with (for example) : x.patchValue({ unit_price: selectedIngredient.price});x.get('unit_...
 angular                     2 answers                     56 view