【发布时间】:2019-03-26 21:41:59
【问题描述】:
我正在制作一个能够发布和附加图片和视频的社交应用程序。
我注意到,如果我尝试上传大文件,那么 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
}
})
}
如果我上传
更新 1
这是因为 Alamofire 的大小限制:
MAhipal Singh 建议使用 Stream 可以解决此问题:Alamofire upload huge file
但我真的不明白..
【问题讨论】:
-
你的PHP能否接受大尺寸上传,问题可能出在php端而不是iOS
-
@Tobi 是的,PHP 能够处理最大 25MB 的文件
标签: php swift parameters upload alamofire