使用AFNetworking Multipart表单数据无法正常运行多个图片

时间:2018-06-11 14:17:44

标签: ios swift image-uploading multipartform-data afnetworking-2

我正在尝试使用我的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共享类。

初始化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)

请帮忙解决这个问题。

1 个答案:

答案 0 :(得分:0)

最终解决了该问题。在服务器端这是一个问题。有用于记录从应用接收的请求的代码段。当图像中有大尺寸图像时,此代码将引发错误。删除该代码后,它可以正常工作。希望这对其他人也有帮助。