简体   繁体   中英

How to upload multiple file using alamofire with showing progress?

I want to upload multiple files to server in iOS. I have data as NSData and I want to upload it to server. I am using alamofire for uploading the data. I have gone through their documentation but could not find good answer. I am not able to understand how will the code below work with NSData & multiple images. Please provide solution.

let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")

Alamofire.upload(fileURL, to: "https://httpbin.org/post")
    .uploadProgress { progress in // main queue by default
        print("Upload Progress: \(progress.fractionCompleted)")
    }
    .downloadProgress { progress in // main queue by default
        print("Download Progress: \(progress.fractionCompleted)")
    }
    .responseJSON { response in
        debugPrint(response)
    }

Use the following code for multiple image uploads using Alamofire. You can find documentation around it here : -

Alamofire.upload(
multipartFormData: { multipartFormData in
    multipartFormData.append(data, name: "imageFile",
                 fileName: "image.jpg", mimeType: "image/jpeg"), 
    multipartFormData.append(data1, name: "image1File",
                fileName: "image1.jpg", mimeType: "image/jpeg")
},
to: "https://httpbin.org/post", //URL,
headers: headers,
encodingCompletion: { encodingResult in
    switch encodingResult {
    case .success(let upload, _, _):
        upload.responseJSON { response in
            debugPrint(response)
        }
        upload.uploadProgress(closure: { //Get Progress
                progress in
                    print(progress.fractionCompleted)
            })
    case .failure(let encodingError):
        print(encodingError)
    }
})

Note: This is using Alamofre 4.0

For Multiple image upload with progress ios swift

   Alamofire.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(data, withName: "file", fileName: "image.jpg", mimeType:"image/jpeg")
            multipartFormData.append(data1, withName: "file", fileName: "image1.jpg", mimeType:"image/jpeg")
    },
    to: url, method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.uploadProgress(closure: { (progress) in
                let progressVal = String(format: "%.2f%%", progress.fractionCompleted * 100)
                print("\(progressVal)")
            })
            
            upload.responseJSON{ response in
                print(response)
                
            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")
           
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM