虽然目前绝大部分的存储服务都是用了云存储,直接传上了云,然后拿到一个
resource-key
扔给自己的服务器,而且在传输到云的时候直接调用了相关云提供的SDK
中的一个API
,过程是如此的简单,但是作为一个专业的开发者,我们必须要知道这里面所涉及的知识,必须要明白这里面的逻辑。用过AFNetworking
的筒子们,只要做过有直传头像之类的直接到自己服务器的时候都用过:- (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(id)parameters constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress success:(void (^)(NSURLSessionDataTask *task, id responseObject))success failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
这个方法,那么在Alamofire中如何做多表单上传的,以及内部原理是怎样的,本篇文章带着大家一起深入探究一下。
先看了简单的纯key-value的multipartFormData
上传:
SessionManager.default
.upload(multipartFormData: { (mutilPartData) in
mutilPartData.append("BoxJing".data(using: .utf8)!, withName: "name")
mutilPartData.append("boy".data(using: .utf8)!, withName: "gender")
mutilPartData.append("2016-08-25".data(using: .utf8)!, withName: "birthday")
}, to: "http://www.fotoplace.cc") { (result) in
}
在Charles中可以看到传输的内容:
这些东西是如何转换塞进
HTTPBody
中的,仔细的看一看Charles中拦截到的内容和代码里咱们自己设置的key-value,大致的东西就是:
--boundary
Content-Dispositon>>name="name" (key)
\r\n 换行
BoxJing (value)
--boundary
。。。
-
--boundary--
格式非常的有规律!直接去看upload
的源码:
open func upload(
multipartFormData: @escaping (MultipartFormData) -> Void,
usingThreshold encodingMemoryThreshold: UInt64 = SessionManager.multipartFormDataEncodingMemoryThreshold,
with urlRequest: URLRequestConvertible,
queue: DispatchQueue? = nil,
encodingCompletion: ((MultipartFormDataEncodingResult) -> Void)?)
{
DispatchQueue.global(qos: .utility).async {
let formData = MultipartFormData()
multipartFormData(formData)
var tempFileURL: URL?
do {
var urlRequestWithContentType = try urlRequest.asURLRequest()
urlRequestWithContentType.setValue(formData.contentType, forHTTPHeaderField: "Content-Type")
let isBackgroundSession = self.session.configuration.identifier != nil
if formData.contentLength < encodingMemoryThreshold && !isBackgroundSession {
let data = try formData.encode()
let encodingResult = MultipartFormDataEncodingResult.success(
request: self.upload(data, with: urlRequestWithContentType),
streamingFromDisk: false,
streamFileURL: nil
)
(queue ?? DispatchQueue.main).async { encodingCompletion?(encodingResult) }
} else {
let fileManager = FileManager.default
let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory())
let directoryURL = tempDirectoryURL.appendingPathComponent("org.alamofire.manager/multipart.form.data")
let fileName = UUID().uuidString
let fileURL = directoryURL.appendingPathComponent(fileName)
tempFileURL = fileURL
var directoryError: Error?
// Create directory inside serial queue to ensure two threads don't do this in parallel
self.queue.sync {
do {
try fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true, attributes: nil)
} catch {
directoryError = error
}
}
if let directoryError = directoryError { throw directoryError }
try formData.writeEncodedData(to: fileURL)
let upload = self.upload(fileURL, with: urlRequestWithContentType)
// Cleanup the temp file once the upload is complete
upload.delegate.queue.addOperation {
do {
try FileManager.default.removeItem(at: fileURL)
} catch {
// No-op
}
}
(queue ?? DispatchQueue.main).async {
let encodingResult = MultipartFormDataEncodingResult.success(
request: upload,
streamingFromDisk: true,
streamFileURL: fileURL
)
encodingCompletion?(encodingResult)
}
}
} catch {
// Cleanup the temp file in the event that the multipart form data encoding failed
if let tempFileURL = tempFileURL {
do {
try FileManager.default.removeItem(at: tempFileURL)
} catch {
// No-op
}
}
(queue ?? DispatchQueue.main).async { encodingCompletion?(.failure(error)) }
}
}
}
看到这个源码感觉是有很多内容,其实很多东西都是一些细节处理,重要的代码就那几行,首先看multipartFormData(formData)
,为什么先看它,我们在最外层调用SessionManager.default .upload(multipartFormData: { (mutilPartData) in
的时候先要进行数据处理,就是调用的multipartFormData
的代码块,所以直接干进multipartFormData(formData)
看一看里面的处理,点进去看到MultipartFormData
类的源码中有二段:
struct EncodingCharacters {
static let crlf = "\r\n"
}
struct BoundaryGenerator {
enum BoundaryType {
case initial, encapsulated, final
}
static func randomBoundary() -> String {
return String(format: "alamofire.boundary.%08x%08x", arc4random(), arc4random())
}
static func boundaryData(forBoundaryType boundaryType: BoundaryType, boundary: String) -> Data {
let boundaryText: String
switch boundaryType {
case .initial:
boundaryText = "--\(boundary)\(EncodingCharacters.crlf)"
case .encapsulated:
boundaryText = "\(EncodingCharacters.crlf)--\(boundary)\(EncodingCharacters.crlf)"
case .final:
boundaryText = "\(EncodingCharacters.crlf)--\(boundary)--\(EncodingCharacters.crlf)"
}
return boundaryText.data(using: String.Encoding.utf8, allowLossyConversion: false)!
}
}
在类中直接搞一个结构体,保存了一个属性,这种操作在前面介绍DataRequest
时候的DataRequest.Requestable
已经见到过了,不会感到陌生。BoundaryType
枚举中的三个值,代表着什么,字面意思就是初始,结束和中间部分,在后面的拼接中一定会有所判断,BoundaryGenerator
中的randomBoundary
方法,不就是用来生成Charles中类似alamofire.boundary.6ac5c0cebdbf1af7
的吗?boundaryData
方法中根据BoundaryType
枚举来判断是开始还是结尾,还是在中间的拼接的字符串。仔细比对后格式就是我们在Charles中抓到的一毛一样。我们从append追踪进去:
public func append(_ data: Data, withName name: String) {
let headers = contentHeaders(withName: name)
let stream = InputStream(data: data)
let length = UInt64(data.count)
append(stream, withLength: length, headers: headers)
}
private func contentHeaders(withName name: String, fileName: String? = nil, mimeType: String? = nil) -> [String: String] {
var disposition = "form-data; name=\"\(name)\""
if let fileName = fileName { disposition += "; filename=\"\(fileName)\"" }
var headers = ["Content-Disposition": disposition]
if let mimeType = mimeType { headers["Content-Type"] = mimeType }
return headers
}
contentHeaders
方法的作用直接来个图大家就明白是干嘛用的了:
直接进到
append
方法内部:
public func append(_ stream: InputStream, withLength length: UInt64, headers: HTTPHeaders) {
let bodyPart = BodyPart(headers: headers, bodyStream: stream, bodyContentLength: length)
bodyParts.append(bodyPart)
}
class BodyPart {
let headers: HTTPHeaders
let bodyStream: InputStream
let bodyContentLength: UInt64
var hasInitialBoundary = false
var hasFinalBoundary = false
init(headers: HTTPHeaders, bodyStream: InputStream, bodyContentLength: UInt64) {
self.headers = headers
self.bodyStream = bodyStream
self.bodyContentLength = bodyContentLength
}
}
比较清楚了,代码块中不断的append就是把需要的内容全部处理后放进bodyParts
这个数组中,那么在后面的某一步,肯定会从这个数组中取出来进行再次处理,我们直接看源码的let data = try formData.encode()
,看这个方法名字是不是就像是要处理一遍的节奏:
public func encode() throws -> Data {
if let bodyPartError = bodyPartError {
throw bodyPartError
}
var encoded = Data()
bodyParts.first?.hasInitialBoundary = true
bodyParts.last?.hasFinalBoundary = true
for bodyPart in bodyParts {
let encodedData = try encode(bodyPart)
encoded.append(encodedData)
}
return encoded
}
bodyParts.first
、bodyParts.last
不就是数组中的第一个和最后一个吗,设置不同的标识,在后面拼接的时候会特殊处理,for bodyPart in bodyParts
就是遍历了这个数组把每一项进行encode
处理:
private func encode(_ bodyPart: BodyPart) throws -> Data {
var encoded = Data()
let initialData = bodyPart.hasInitialBoundary ? initialBoundaryData() : encapsulatedBoundaryData()
encoded.append(initialData)
let headerData = encodeHeaders(for: bodyPart)
encoded.append(headerData)
let bodyStreamData = try encodeBodyStream(for: bodyPart)
encoded.append(bodyStreamData)
if bodyPart.hasFinalBoundary {
encoded.append(finalBoundaryData())
}
return encoded
}
源码里有一句:let bodyStreamData = try encodeBodyStream(for: bodyPart)
,这个东西是什么时候存在了bodyPart
里面的?在前面bodyParts.append(bodyPart)
,数组里添加这个对象的初始化的时候BodyPart(headers: headers, bodyStream: stream, bodyContentLength: length)
就传进去了的。这里用数据流而不直接用data的原因是data不方便传输,而且对内存压力也高,用户数据流的方式就会节省内存,这么优秀的框架肯定会从用户的角度来来考虑,采用省内存的方式来处理。所有的数据拼接完成后开始真正的上传:
open func upload(_ data: Data, with urlRequest: URLRequestConvertible) -> UploadRequest {
do {
let urlRequest = try urlRequest.asURLRequest()
return upload(.data(data, urlRequest))
} catch {
return upload(nil, failedWith: error)
}
}
private func upload(_ uploadable: UploadRequest.Uploadable) -> UploadRequest {
do {
let task = try uploadable.task(session: session, adapter: adapter, queue: queue)
let upload = UploadRequest(session: session, requestTask: .upload(uploadable, task))
if case let .stream(inputStream, _) = uploadable {
upload.delegate.taskNeedNewBodyStream = { _, _ in inputStream }
}
delegate[task] = upload
if startRequestsImmediately { upload.resume() }
return upload
} catch {
return upload(uploadable, failedWith: error)
}
}
看到最后的let upload = UploadRequest(session: session, requestTask: .upload(uploadable, task))
和upload.resume()
不就回到了之前的Request篇章所介绍的内容了,在UploadRequest
源码里:
switch self {
case let .data(data, urlRequest):
let urlRequest = try urlRequest.adapt(using: adapter)
task = queue.sync { session.uploadTask(with: urlRequest, from: data) }
case let .file(url, urlRequest):
let urlRequest = try urlRequest.adapt(using: adapter)
task = queue.sync { session.uploadTask(with: urlRequest, fromFile: url) }
case let .stream(_, urlRequest):
let urlRequest = try urlRequest.adapt(using: adapter)
task = queue.sync { session.uploadTask(withStreamedRequest: urlRequest) }
}
这里就回到了Session
的uploadTask
方法,分了3种上传方式data
、file
和stream
。
简单总结一下,整个多表单上传的过程:
我们的数据
->append
(进行拼接)
->bodyParts
(存储多个append数据的数组)
->encode
(内部处理)
->data
(处理后的二进制)
->request
(接收处理后数据的请求)
->session
(真正执行request的session.uploadTask)
网友评论