【问题标题】:using Alamofire 5 to upload file to RocketChat server使用 Alamofire 5 上传文件到 RocketChat 服务器
【发布时间】:2020-10-11 00:36:36
【问题描述】:

我正在使用 Alamofire 5,并且正在尝试将图像上传到 Rocket Chat 服务器。我需要使用 AF 复制的相应 curl 语句位于以下链接: (文档链接:https://docs.rocket.chat/api/rest-api/methods/rooms/upload

我一直在尝试使用 multipartFormData 上传,但没有成功。我还尝试完全绕过 Alamofire 并使用 Swift URLSession。我能做的最好的就是从服务器获得相同的错误消息,即“errorType”:invalid-field。”

我现在的代码:

let url = URL_MESSAGES + "rooms.upload/\(_group._id ?? "")"
    
let boundary = UUID().uuidString

let headers: HTTPHeaders = [
    
"X-Auth-Token": authToken,
    "X-User-Id": me._id ?? "",
    "Content-type":"multipart/form-data; boundary=\(boundary)"
]

if let data = image.jpeg(.medium) {
        
    print(data.count)
        
    AF.upload(
        multipartFormData: { multipartFormData in
                multipartFormData.append(data, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
        },
            to: url, method: .post , headers: header)
            .response { resp in
                print(resp)

        }
        .cURLDescription { description in
            print(description)
        }
        .responseString { [weak self] (response) in
            
            DispatchQueue.main.async {                 
                if response.error == nil {
                    
                    guard let data = response.data else {
                        return completion(true,[:])
                    }

                    if let json = try? JSON(data: data) {

                        let dictionaryIn = json.rawValue as! [String : Any]

                        if (self?.isSuccess(data: dictionaryIn))! {
                            completion(true,json.rawValue as! [String : Any])
                        }else{
                            completion(false,[:])
                            self?.handleError(data: dictionaryIn)
                        }
                    }

                }else{
                        completion(false,[:])
                        self?.handleError(data: [:])
                }
            }
        }
    }
}

【问题讨论】:

    标签: swift alamofire rocket.chat


    【解决方案1】:

    我认为您通过设置自己的边界来破坏上传。 Alamofire 会自动为您服务。尝试删除标题。

    【讨论】:

    • 我这样做了,但我得到了同样的错误:dataError:["errorType": invalid-field, "success": 0, "error": [invalid-field]]
    【解决方案2】:

    上面的代码完美运行,当我改变这个时:

     AF.upload(
        multipartFormData: { multipartFormData in
                multipartFormData.append(data, withName: "image", fileName: "image.jpeg", mimeType: "image/jpeg")
    

    到:

     AF.upload(
        multipartFormData: { multipartFormData in
                multipartFormData.append(data, withName: "file", fileName: "image.jpeg", mimeType: "image/jpeg")
    

    将“图像”替换为“文件”修复了所有问题。我选择了“图像”,因为这是我能找到的每个 Alamofire 教程都说要做的。但是,RocketChat 要求它是“文件”。它在文档中,但我没有意识到这是它告诉我要做的。具体来说,在文档中,它说:

    "注意:对于某些文件类型,如果通过 curl 上传,您可能需要设置 mime 类型。 对于某些文件类型,curl 会将文件上传为 application/octet-stream。您可以像这样传递自定义 mime 类型: -F "file=@file.wav;type=audio/wav" "

    从 Alamofire 翻译时,我尝试使用 -F "image=@file.wav;type=audio/wav"。它需要是:-F "file=@file.wav;type=audio/wav"

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 1970-01-01
      • 2017-12-23
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 2018-01-19
      • 2016-06-07
      • 1970-01-01
      相关资源
      最近更新 更多