使用 AFNetworking Multipart 表单数据上传多个图像不起作用

Multiple Image upload using AFNetworking Multipart form data not working

我正在尝试在我的 Swift 项目

中使用 AFNetworking 2.6.3 上传多张图像作为数据的多部分

这是我将数据附加到表单数据的代码

self.webserviceHandler.post(serviceName, parameters: nil, constructingBodyWith: { (data:AFMultipartFormData) in
                for i in 0..<imageArray.count{
                    let imagData = UIImageJPEGRepresentation(imageArray[i], 0.5)
                    data.appendPart(withFileData: imagData!, name: "MenaImage[]", fileName: fileNameArray[i], mimeType: "image/jpeg")

                }

                for (key, value) in parameters{
                    data.appendPart(withForm: "\(value)".data(using: .utf8)!, name: key as! String)
                }
            }, success: { (operation:AFHTTPRequestOperation!, responseObject:Any!) in
                successResponseClosure(responseObject as AnyObject)
            }) { (operation:AFHTTPRequestOperation!, error:Error!) in
                    errorClosure(error as NSError)
                }

webserviceHandler 是 AFHTTPRequestOperationManager 共享的 class。

AFHTTPRequestOperationManager 的初始化

init() {
        let baseURL = URL(string:"http://test.com/")!
        self.webserviceHandler = AFHTTPRequestOperationManager(baseURL:baseURL)
        self.webserviceHandler.requestSerializer = AFJSONRequestSerializer()
        self.webserviceHandler.responseSerializer = AFJSONResponseSerializer()

        let contentType = NSString(string:"text/html")
        let contentTypes = NSMutableSet(set:self.webserviceHandler.responseSerializer.acceptableContentTypes!)
        if contentTypes.contains(contentType) == false{
            contentTypes.add(contentType)
            self.webserviceHandler.responseSerializer.acceptableContentTypes = contentTypes as Set<NSObject>

        }

这是 AFNetworkActivityLogger 记录的请求

{
    "Accept-Language" = "en-BH;q=1, ar-BH;q=0.9, en-GB;q=0.8";
    "Content-Length" = 3023913;
    "Content-Type" = "multipart/form-data; boundary=Boundary+F955A845D801084F";
    "User-Agent" = "MBB/1.0 (iPhone; iOS 11.2.5; Scale/2.00)";
} (null)

请帮忙解决这个问题。

问题终于解决了。这是服务器端的问题。有代码片段来记录从应用程序接收的请求。当请求中的图像有大尺寸图像时,此代码会抛出错误。删除该代码后,它可以完美运行。希望这对其他人也有帮助。