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.
UPDATE 1
It is because of the Alamofire size limit:
MAhipal Singh suggested that with a Stream it could be solved: Alamofire upload huge file
But I don't really understand it..
Without seeing error codes it’s hard to tell , however I’m sure the limitation is in your php setup, check yourphp.ini
for upload max file size and perhaps post max size values. You’ll find out it’s most probably 10 mb.
Bumping that up to a higher value will work, but I suggest you do more research on how that will affect your server.