使用Alamofire使用绝对简单的代码上传到服务器

发布于 2025-02-05 12:07:35 字数 826 浏览 3 评论 0原文

我想用我能理解的最简单的代码上传选定的图像到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2025-02-12 12:07:35

我只想让每个人都知道,看似问题是我无法将与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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文