使用多部分和不记名令牌上传图像

Posted

技术标签:

【中文标题】使用多部分和不记名令牌上传图像【英文标题】:Image Upload with multipart and bearer token 【发布时间】:2020-06-07 19:09:54 【问题描述】:

我尝试在服务器中上传图片。它在邮递员中工作。但从我的代码来看,它的显示总是验证失败。

func uploadPhoto( image: UIImage, completion: @escaping (JSON) -> ()) 


        let url = "\(AppConstant.imageUpload)"


//        let headers: HTTPHeaders
//        headers = [
//            "Authorization": "Bearer \(AppConstant.token))"
//            ]


           let headers: HTTPHeaders = [
                "Content-type": "multipart/form-data",
                "Content-Disposition" : "form-data",
                "Authorization": "Bearer \(AppConstant.token))"
           ]


//       let headers: HTTPHeaders = [
//                      "Content-type": "multipart/form-data",
//                      "Content-Disposition" : "form-data",
//                      "Authorization": "Bearer \(AppConstant.token))"
//                 ]
//

//
//     let headers: HTTPHeaders = [
//                   "Content-Type": "application/json",
//                   "Content-Disposition" : "form-data",
//                   "Authorization": "Bearer \(AppConstant.token))"
//               ]


//        let headers: HTTPHeaders = [
//                   "Content-type": "application/form-data",
//                   "Authorization": "Bearer \(AppConstant.token))"
//               ]
//        let headers: HTTPHeaders = [
//                          "Content-Type": "application/json",
//                         "Authorization": "Bearer \(AppConstant.token))"
//                     ]
//
        print("token  \(AppConstant.token)    url:  \(url)")


       // let httpHeaders = HTTPHeaders(headers)
        AF.upload(multipartFormData:  multiPart in

           multiPart.append(image.jpegData(compressionQuality: 0.4)!, withName: "image", fileName: "file.jpg", mimeType: "image/jpg")
        , to: url, method: .post, headers: headers) .uploadProgress(queue: .main, closure:  progress in
            print("Upload Progress: \(progress.fractionCompleted)")
        ).responseString(completionHandler:  data in
            print("upload finished: \(data.data)")
        ).response  (response) in
            print("upload responce:  \(response.data)")

            switch response.result 
            case .success(let resut):

                DispatchQueue.main.async 
                                 do


                                 //   self.imageUrl = try JSONDecoder().decode(ImageUploadUrl.self, from: resut!)

                                    if let data = resut,
                                           let urlContent = NSString(data: data, encoding: String.Encoding.ascii.rawValue) 
                                           print("urlContent  \(urlContent)")
                                        else 
                                           print("Error: ")
                                       
                                    print("upload 10 \(self.imageUrl?.url)")


                                               catch 
                                    print("error occur ")
                                 
                             
                print("upload success result: \(resut)")
            case .failure(let err):
                print("upload err: \(err)")
            
        
    

【问题讨论】:

您在使用 AFNetworking 吗?那么您使用的是哪个版本的 AFNetworking? 【参考方案1】:

我发现您在标题中输入错误: 你写的标题如下:

let headers: HTTPHeaders = [
       "Content-type": "multipart/form-data",
       "Content-Disposition" : "form-data",
       "Authorization": "Bearer \(AppConstant.token))"
     ]

但你必须删除标记最后的括号,所以你的代码应该是这样的

let headers: HTTPHeaders = [
       "Content-type": "multipart/form-data",
       "Content-Disposition" : "form-data",
       "Authorization": "Bearer \(AppConstant.token)"
     ]

希望能帮到你

【讨论】:

感谢您的帮助...过去 5 小时搜索我的错误但未找到。

以上是关于使用多部分和不记名令牌上传图像的主要内容,如果未能解决你的问题,请参考以下文章

使用 Alamofire 和不记名令牌序列化对象

用户名/密码和不记名令牌之间的区别是啥?

编写可通过 asp.net 身份用户和不记名令牌访问的单个 API

Angular 中的不记名令牌

使用多部分和 Alamofire 上传图像

Android 相机图像未上传到服务器。使用多部分数据 Http post