我下面有这种代码

func upload(){
    let img = UIImage(named: "1")
    let imgData = UIImageJPEGRepresentation(img!, 1)
    let data = imgData?.base64EncodedString()

    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("\(auth)", forHTTPHeaderField: "Authorization")
    request.setValue("application/xml", forHTTPHeaderField: "Content-Type")

    var postString = "<uploadrequest>"
    postString += "<folderid>123</folderid>"
    postString += "<folder_name>images</folder_name>"
    postString += "<image_byte>\(data!)</image_byte>"
    postString += "</uploadrequest>"

    request.httpBody = postString.data(using: .utf8)

    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)

    let task = session.uploadTask(with: request, from: imgData!, completionHandler: {
        (data, response, error) -> Void in

        if let error = error {
            print(error)
            return
        }
        if let data = data {
            _ = self.parseJsonData(data: data)
        }
    })

    task.resume()
}

func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
    print("sent:", totalBytesSent, "from:", totalBytesExpectedToSend)

    let size = totalBytesSent / totalBytesExpectedToSend
    self.progressbar.progress = Float(size)
}

我的目标是使用API​​上传图像并获得其上传进度。该图像将转换为base64,并插入到API的字符串后的<image_byte>\(data!)</image_byte>处。

然后我得到的是这样的:

发送:32768从:927220
发送:65536从:927220
发送:131072从:927220
发送:163840从:927220
发送:262144从:927220
发送:294912来自:927220
发送:360448从:927220
发送:393216来自:927220
发送:425984从:927220
发送:491520来自:927220
发送:524288来自:927220
发送:557056从:927220
发送:589824从:927220
发送:622592从:927220
发送:655360从:927220
发送:688128从:927220
发送:720896从:927220
发送:786432从:927220
发送:819200从:927220
发送:851968来自:927220
发送:927220从:927220
错误域= NSCocoaErrorDomain代码= 3840“JSON文本不是以数组或对象开头,并且没有允许设置片段的选项。”
UserInfo = {NSDebugDescription = JSON文本不是以数组开头或
对象和允许未设置片段的选项。}

但是,如果我正在使用其他类似的方法(没有uploadTask),则效果很好,但没有上传进度。
let task = URLSession.shared.dataTask(with: request, completionHandler: {
        (data, response, error) -> Void in

        if let error = error {
            print(error)
            return
        }
        if let data = data {
            _ = self.parseJsonData(data: data)
        }
    })

我也尝试过Alamofire。带有Alamofire函数的request是成功的,但是仍然没有上传进度,带有Alamofire函数(multipartformData)的upload导致此错误:

responseSerializationFailed(原因:Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(错误:Error Domain = NSCocoaErrorDomain代码= 3840“字符3无效值。” UserInfo = {NSDebugDescription =字符3无效值)})

我想知道我的代码是否有误。您能告诉我我该如何处理我的代码吗?还是问题出在我的API上,因为我的API只接受base64代码?

如果您想查看我的Alamofire代码,那就是there

拜托,有人帮我解决这个问题。

谢谢! :)

编辑1

这是我通过请求功能(没有上传功能)得到的结果
{
    "media_preview" =     {
        "image_media" =         {
            fileid = 4928501;
            filename = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            folderpath = "product\\66861/images800\\";
            height = 533;
            src = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            width = 800;
        };
        "medium_media" =         {
            fileid = 4928503;
            filename = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            folderpath = "product\\66861/images500\\";
            height = 333;
            src = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            width = 500;
        };
        "small_media" =         {
            fileid = 4928503;
            filename = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            folderpath = "\product\\66861/images300\\";
            height = 200;
            src = "/zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            width = 300;
        };
        "source_media" =         {
            fileid = 4928499;
            filename = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            folderpath = "\\product\\66861images\\";
            height = 666;
            src = "/zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            width = 999;
        };
        "thumbnail_media" =         {
            fileid = 4928507;
            filename = "zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            folderpath = \\product\\66861/thumb\\";
            height = 150;
            src = "/zhzvoHBb1ogda9bps13R5IavYiadCm.jpg";
            width = 150;
        };
    };
    responsestatus =     {
        "access_token" = "<null>";
        code = 00;
        description = Success;
    };
}

编辑2
这是parseJsonData(data: Data)的代码
func parseJsonData(data: Data) {
    do {
        let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
        print(jsonResult!)
    } catch {
        print(error)
    }
}

最佳答案

对于具有大量参数的图像正在上传到服务器。尝试使用此代码 Alamofire 。它要工作。

Alamofire.upload(multipartFormData: { (multipartFormData : MultipartFormData) in
            let count = imageToUpload.count
            for (key, value) in parameters {
                multipartFormData.append((value as AnyObject).data(using: String.Encoding.utf8.rawValue)!, withName: key)
            }
            for i in 0..<count{
                let image:UIImage = self.loadImage(imageToUpload[i])
                if let imageData = image.jpegData(compressionQuality: 0.5) {
                    let imageName  = "image[\(i)]"
                    multipartFormData.append(imageData as Data, withName:imageName , fileName: imageToUpload[i], mimeType: "image/jpeg")
                }
            }
        }, usingThreshold: UInt64.init(), to: serviceUrl, method: .post, headers: headers)  { (result) in
            switch result {
            case .success(let upload, _ , _):
                upload.uploadProgress(closure: { (progress) in
                    print("uploding: \(progress.fractionCompleted)")
                })
                upload.responseJSON { response in
                    if response.result.isFailure {
                        if let err = response.error{
                            print(err)
                        }
                        return
                    }

                    guard let jsonData = response.result.value else {
                        return
                    }
                    //

                    do{
                        let json = try JSONSerialization.jsonObject(with: response.data as! Data, options: [])
                        print("josnResposne:\(json)")

                    } catch let parsingError {
                        print("Error", parsingError)
                    }

                }
            case .failure(let encodingError):
                print("failed")
                print(encodingError)
            }
        }

关于ios - Swift 4-使用URLSession获取上传图像进度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53650584/

10-15 13:41