Thursday, 25 October 2018

Alamofire Swift: upload more than 10mb file?

I'm making a social application with the ability to post and attach images & videos.

I've noticed that if I try to upload heavy files then the PHP will not get some parameters (userId and session for example).

Alamofire allows only 10mb of file being uploaded without a stream.

My question is, how could I rewrite this code to be able to upload more images / videos at the same time heavier than 10mb overall?

Here's the code for the posting:

func post(message: String, type: Int, duration: Int, pickedFiles: [Any], completion: @escaping (ActionResult?, Int?, String?, Int?, Int?, String?)->()){

        var pickedVideoUrls : [URL] = []
        var pickedImages : [UIImage] = []
        for file in pickedFiles {
            if let image = file as? UIImage {
                pickedImages.append(image)
            } else if let videoUrl = file as? URL {
                pickedVideoUrls.append(videoUrl)
            }
        }
        let userId = UserData.shared.details.userId
        let session = UserData.shared.details.session

        if (latitude == nil || longitude == nil){
            return completion(ActionResult(type: 0, title: NSLocalizedString("error", comment: ""), message: NSLocalizedString("err_locServicesFail", comment: "")), nil, nil, nil, nil, nil)
        }

        let connectUrl = URL(string: appSettings.url + "/src/main/post.php")



        Alamofire.upload( multipartFormData: { multipartFormData in
            multipartFormData.append("\(userId)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "userId")
            multipartFormData.append(session.data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "session")
             multipartFormData.append(message.data(using: String.Encoding.utf8, allowLossyConversion: true)!, withName: "message")
             multipartFormData.append("\(type)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "type")
             multipartFormData.append("\(duration)".data(using: String.Encoding.utf8, allowLossyConversion: false)!, withName: "duration")

            // Now upload the videos & images
            var fileNumber = 0
            for file in pickedFiles {
                if let image = file as? UIImage {
                    let imgData = UIImageJPEGRepresentation(image, 0.2)!
                    multipartFormData.append(imgData, withName: "image[]", fileName: "file.\(fileNumber).png", mimeType: "image/png")
                    fileNumber+=1
                } else if let videoUrl = file as? URL {
                    multipartFormData.append(videoUrl, withName: "video[]", fileName: "file.\(fileNumber).mp4", mimeType: "video/mp4")
                    fileNumber+=1
                }
            }



        }, to: connectUrl!, encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    // EVERYTHING WAS FINE
                }
            case .failure(let encodingError):

                // ERROR
            }
        })
    }

If I upload small files <10mb it works fine.



from Alamofire Swift: upload more than 10mb file?

No comments:

Post a Comment