How to upload an arbitrary number of files with Swift and Alamofire? -
i'm trying access rest api lets me upload different number of files, depending on situation. i've got following code alamofire i'm not sure how change can upload 1 file, 2 files, or ten files.
alamofire.upload( .post, "https://httpbin.org/post", multipartformdata: { multipartformdata in multipartformdata.appendbodypart(fileurl: farmfileurl, name: "xml-file-farm") multipartformdata.appendbodypart(fileurl: farmfileurl, name: "csv-measurement-file-1") multipartformdata.appendbodypart(fileurl: farmfileurl, name: "csv-measurement-file-2") }, encodingcompletion: { encodingresult in switch encodingresult { case .success(let upload, _, _): upload.responsejson { response in debugprint(response) } case .failure(let encodingerror): print(encodingerror) } } )
the problem see can't define array of files using 'multipartformdata' because doesn't exist until you're inside 'upload' method.
make array of tuple contains filename
, url
, pass it:
func uploadfiles(files:[(string,nsurl)]){ alamofire.upload( .post, "https://httpbin.org/post", multipartformdata: { multipartformdata in (filename, fileurl) in files{ multipartformdata.appendbodypart(fileurl: fileurl, name: filename) } }, encodingcompletion: { encodingresult in switch encodingresult { case .success(let upload, _, _): upload.responsejson { response in debugprint(response) } case .failure(let encodingerror): print(encodingerror) } } ) }
Comments
Post a Comment