如何通过swift中的alamofire上传从手机中挑选的pdf和图像(任何一个选择的任何一个)文件

问题描述 投票:0回答:1

这是调用选择文件的函数。需要发送参数。请求网址是服务器网址。

func Upload()
    { 
        var request = URLRequest(url: URL(string: Constants.mainApi + 
        Constants.ADD_CLAIM_DOC)!)
        request.httpMethod = "POST"
        request.setValue("application/x-www-form-urlencoded", 
        forHTTPHeaderField: "Content-Type")
        request.setValue("Bearer \(userToken)", forHTTPHeaderField: 
         "Authorization")

        let parameters = ["FileName": myURL as AnyObject,
                              "ClaimID":self.ClaimID!
                ] as [String : Any]
    }
swift image pdf upload alamofire
1个回答
0
投票

function UploadDoc(){

    let URL1 = URL(string: Constants.mainApi + Constants.ADD_CLAIM_DOC)!

    let filename = myURL?.lastPathComponent
    Alamofire.upload(
        multipartFormData: { multipartFormData in
            multipartFormData.append(self.myURL!, withName: filename)
            multipartFormData.append((self.ClaimID?.data(using: .utf8)!)!, withName: "ClaimID")

    },
        to: URL1,
        method : .post,
        headers : Constants.headers,
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseJSON { response in
                    debugPrint(response)


            case .failure(let encodingError):
                print(encodingError)

    })


}
© www.soinside.com 2019 - 2024. All rights reserved.