我正在制作具有发布和附加图像和视频功能的社交应用程序。
我注意到,如果我尝试上传大量文件,则PHP将无法获得某些参数(例如userId
和session
)。
Alamofire仅允许10mb的文件上传而没有流。
我的问题是,我该如何重写这段代码,以便能够同时上传更多的图像/视频,而总重不超过10mb?
以下是发布的代码:
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
}
})
}
如果我上传的文件小于10mb,则可以正常工作。
更新1
这是由于Alamofire尺寸限制:
MAhipal Singh建议使用Stream可以解决:Alamofire upload huge file
但是我不太了解。
答案 0 :(得分:2)
虽然看不到错误代码,但很难分辨,但是我确定您的php设置存在限制,请检查您的php.ini
上载最大文件大小,并发布最大大小值。您会发现它最大可能为10 mb。
将其提高到更高的值是可行的,但是我建议您对这将如何影响服务器进行更多研究。