Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

ios - Swift 4 - Get an Upload Image Progress using URLSession

I have this kind of code below

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

My target is to upload an image using API and get its upload progress too. The image is converted into base64 and inserted at <image_byte>(data!)</image_byte> on the poststring of the API.

Then what I got was something like this:

sent: 32768 from: 927220 sent: 65536 from: 927220 sent: 131072 from: 927220 sent: 163840 from: 927220 sent: 262144 from: 927220 sent: 294912 from: 927220 sent: 360448 from: 927220 sent: 393216 from: 927220 sent: 425984 from: 927220 sent: 491520 from: 927220 sent: 524288 from: 927220 sent: 557056 from: 927220 sent: 589824 from: 927220 sent: 622592 from: 927220 sent: 655360 from: 927220 sent: 688128 from: 927220 sent: 720896 from: 927220 sent: 786432 from: 927220 sent: 819200 from: 927220 sent: 851968 from: 927220 sent: 927220 from: 927220 Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

But if I am using other methods like this (without uploadTask), it works good but got no upload progress.

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

I've tried Alamofire too. Alamofire with request function was success but still gave no upload progress and Alamofire with uploadfunction (multipartformData) make this error:

responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 3." UserInfo={NSDebugDescription=Invalid value around character 3.}))

I want to know if I make a mistake on my code. Could you tell me what should I do with my code? Or the problem was my API because my API just accepts a base64 code?

If you want to see my Alamofire code, it was there

Please, somebody help me to solve this problem.

Thanks! :)

EDIT 1

This is my result by the request function (without upload function)

{
    "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;
    };
}

EDIT 2 This is the code of 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)
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For image with multple Paramaters Uploading to server. Try this code Alamofire. Its going to work.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...