使用alamofire上传多个图像会导致重复第一张图像

时间:2017-04-13 12:43:37

标签: ios swift3 alamofire

这是我第一次上传图像数组 我正在使用-c并且上传成功 但我发现它重复了第一张图片

Alamofire

可能是使用for循环追加 let selectedImages = NSMutableArray.init() for img in Photos { // photos = [UIImage]() selectedImages.add(UIImageJPEGRepresentation(img, 0.1)!) } var s:String = mainTitleTextfield.text! if s == "" || s == " " { let f = DateFormatter() f.dateFormat = "MMM d, yyyy" let date = Date() s = f.string(from: date) } s = s.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)! let url = serverURL + "postPhotosToClasses/?AlbumName=" + s + "&IsGroup=\(false)" HUD.show(.label("uploading")) Alamofire.upload(multipartFormData: { multipartFormData in for i in 0..<selectedImages.count { multipartFormData.append(selectedImages[i] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg") } multipartFormData.append(selectedImages[1] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg") _ = 3 }, to: url,method:.post, headers:["UserID":"\(currentTeacher.ID)","Ids":arr], encodingCompletion: { encodingResult in switch encodingResult { case .success(let upload, _, _): upload .validate() .responseJSON { response in switch response.result { case .success(let value): print("responseObject: \(value)") HUD.flash(.success, delay: 1.0) case .failure(let responseError): print("responseError: \(responseError)") } } case .failure(let encodingError): print("encodingError: \(encodingError)") } }); }

的问题

1 个答案:

答案 0 :(得分:0)

问题出在withName中 何时更换

for i in 0..<selectedImages.count {
  multipartFormData.append(selectedImages[i] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg")
  }

这条线 通过将withName:"file"更改为withName:"file\(i)"

for i in 0..<selectedImages.count {
  multipartFormData.append(selectedImages[i] as! Data, withName: "file\(i)",fileName: "image.jpg", mimeType: "image/jpeg")  
  }