使用Alamofire使用绝对简单的代码上传到服务器
我想用我能理解的最简单的代码上传选定的图像到ImagePickerController。 (我注意到在Stackoverflow上对类似问题的答案仍然很长,没有太多解释就不容易理解。)绝对没有不需要的代码。因此,我写了下面的代码。但是没有将文件上传到我的服务器。下面我的代码中看起来有问题吗?还是太短了,我实际上缺少什么?还是我应该交叉检查我的服务器代码?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let photoForUpload = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
var photoData = photoForUpload.jpegData(compressionQuality: 0.8)
print(type(of: photoData))
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(photoData!, withName: "fileToUpload")
}, to: "https://www.mywebsite.com/myphpfile.php").response { response in
debugPrint(response)
}
dismiss(animated: true, completion: nil)
}
I want to upload selected image to imagePickerController with absolutely the simplest code that I can understand. (I notice answers to similar questions on stackoverflow are still quite long and not easy to understand without much explanations.) Absolute no codes that are not a must. Hence I wrote the codes below. But no files have been uploaded to my server. Does it look like there are something wrong in my codes below? Or maybe so short that I am actually missing something? Or should I cross-check my server code?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let photoForUpload = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
var photoData = photoForUpload.jpegData(compressionQuality: 0.8)
print(type(of: photoData))
AF.upload(multipartFormData: { multipartFormData in
multipartFormData.append(photoData!, withName: "fileToUpload")
}, to: "https://www.mywebsite.com/myphpfile.php").response { response in
debugPrint(response)
}
dismiss(animated: true, completion: nil)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我只想让每个人都知道,看似问题是我无法将与iPhone照片一样大的文件上传到我从第三方提供商购买该服务的特定服务器上。在我的php
$ _文件
变量提供正确的信息之前,我必须用Swift将图像压缩至0.3质量。看起来很简单。由于我从未在Stackoverflow或Google上看到如此简单的答案,因此我认为在这里分享对我来说是一件好事。谢谢大家的尝试。
I just want to let every one know that the seeming issue is that I cannot upload a file as large as iPhone photo onto my particular server that I bought the service from third-party provider. I have to compress the image to 0.3 quality with SWIFT before my PHP
$_FILES
variable gives correct information. Looks like as simple as that.As I have never seen such a simple answer on StackOverflow or Google, I think it would be good for me to share here. Thanks all for trying.