首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载和上传压缩文件

下载和上传压缩文件
EN

Stack Overflow用户
提问于 2021-08-20 19:11:37
回答 4查看 323关注 0票数 0

我试图模仿以下的卷曲:

代码语言:javascript
复制
curl -v -F file=@/Users/myuser/Downloads/shelly-homekit-Shelly25.zip http://10.0.1.7/update

为了使用curl命令,我下载了zip文件并将其保存到我的计算机上。

我的应用程序应该下载压缩文件,将其存储到设备上,然后上传到服务器。

我尝试将其上传为文件和数据,但都没有成功:

代码语言:javascript
复制
            let destination: DownloadRequest.Destination = { _, _ in
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")

                return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
            }

            AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
                debugPrint(response)

                if response.error == nil, let zipPath = response.fileURL?.path {
                    
                    let url = URL(string: zipPath)!

                    
                    let headers: HTTPHeaders = [
                        .contentType("multipart/form-data")
                    ]
                    
                    if let data = try? Data(contentsOf: url) {
                        AF.upload(data, to: "http://"+dev.ipAddress+"/update",headers: headers).responseDecodable(of: HTTPBinResponse.self) { response in
                            debugPrint(response)
                        }
                    }
                }
            }

我得到以下错误:

谢谢你的帮助

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-08-24 14:28:27

以下代码起作用:

代码语言:javascript
复制
        let data = try? NSData(contentsOf: URL(string:str)!) as Data
    
        AF.upload(multipartFormData: { multipartFormData in
            multipartFormData.append(data!,withName: "file",fileName: file_name ,mimeType: "application/octet-stream")
        }, to: "http://"+dev.ipAddress+"/update")
            .response { response in
                if response.response?.statusCode != 200 {
                    devicesUpdateError.append(dev.name)
                }
            }
票数 0
EN

Stack Overflow用户

发布于 2021-08-21 02:21:49

正如注释中提到的那样,您的方法是允许任意加载。见传输安全阻止了明文HTTP。想了解更多细节

票数 0
EN

Stack Overflow用户

发布于 2021-08-22 07:04:36

这没什么用:

正如注释中提到的那样,您的方法是允许任意加载。请参阅传输安全性阻止了明文HTTP。想了解更多细节

我设置了链接中提到的权限:

权限

但是得到以下错误:

错误

我还对我的代码做了一些修改:

代码语言:javascript
复制
                let destination: DownloadRequest.Destination = { _, _ in
                let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
                let fileURL = documentsURL.appendingPathComponent("shelly-homekit-Shelly1PM.zip")

                return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
            }

            AF.download("https://rojer.me/files/shelly/shelly-homekit-Shelly1PM.zip", to: destination).response { response in
                //debugPrint(response)

                if response.error == nil {
                    AF.upload(response.fileURL!, to: "http://"+dev.ipAddress+"/update").responseDecodable(of: HTTPBinResponse.self) { response_up in
                        debugPrint(response_up)
                    }
                }
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68867084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档