【问题标题】:upload multiple image with alamofire cause repeating first image使用 alamofire 上传多张图片导致重复第一张图片
【发布时间】:2017-09-09 14:24:21
【问题描述】:

这是我第一次上传图片数组 我正在使用Alamofire,上传成功 但我发现它重复了第一张图片

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

        }
    });

}

可能是使用for循环追加multipartFormData的问题

【问题讨论】:

  • multipartFormData.append... for 循环之后是什么意思?
  • 我添加此代码以测试是否存在 foo 循环中的问题,但它会出现同样的问题并上传 selectedimage[0]

标签: ios swift3 alamofire


【解决方案1】:

问题出在 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")  
  }

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 2018-05-31
    • 2017-05-20
    • 2019-11-24
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多