04 - alomafire 文件上传底层探索

71 阅读1分钟
let request =  Session.default.upload(multipartFormData: { multipartData in
    multipartData.append("Zi".data(using: .utf8)!, withName: "fileName")
    multipartData.append("EDU".data(using: .utf8)!, withName: "username")
    multipartData.append("123456".data(using: .utf8)!, withName: "password")
    //multipartData.append(data as! Data, withName: "fileName")
    }, to: urlString, usingThreshold: 10)   
request.response(completionHandler: { response **in**
    **switch** response.result {
    **case** .success:
        print("****:\(response) ****")
    **case** .failure:
        print("****:\(String(describing: response.error)) ****")
    }
})

以下是多表单上传流程图,请求头的拼接、参数的拼接、content-type、boundary等设置。 1、生成boundary分隔符 2、拼接Content-Disposition 3、key拿出来进行拼接 4、拼接/r/n -> crlf(Carriage Return Line Feed) 5、拼接value

WX20220927-140748@2x.png

WX20220927-140808@2x.png