[英]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). 我注意到如果我尝试上传繁重的文件,那么PHP将不会获得一些参数(例如
userId
和session
)。
Alamofire allows only 10mb of file being uploaded without a stream. Alamofire只允许在没有流的情况下上传10mb的文件。
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? 我的问题是,如何重写此代码,以便能够在整体重量超过10mb的同时上传更多图像/视频?
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. 如果我上传小文件<10mb它可以正常工作。
UPDATE 1 更新1
It is because of the Alamofire size limit: 这是因为Alamofire的大小限制:
MAhipal Singh suggested that with a Stream it could be solved: Alamofire upload huge file MAhipal Singh建议用流可以解决: Alamofire上传大文件
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 your php.ini
for upload max file size and perhaps post max size values. 没有看到错误代码很难说,但是我确定你的php设置有限制,检查你的
php.ini
上传最大文件大小,也许发布最大尺寸值。 You'll find out it's most probably 10 mb. 你会发现它最有可能是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. 将这个值提高到更高的值会有效,但我建议您对这将如何影响您的服务器进行更多研究。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.