-
Notifications
You must be signed in to change notification settings - Fork 1
MultipartFormData
Constructs multipart/form-data
for uploads within an HTTP or HTTPS body. There are currently two ways to encode
multipart form data. The first way is to encode the data directly in memory. This is very efficient, but can lead
to memory issues if the dataset is too large. The second way is designed for larger datasets and will write all the
data to a single file on disk with all the proper boundary segmentation. The second approach MUST be used for
larger datasets such as video content, otherwise your app may run out of memory when trying to encode the dataset.
open class MultipartFormData
For more information on multipart/form-data
in general, please refer to the RFC-2388 and RFC-2045 specs as well
and the w3 form documentation.
Creates an instance.
public init(fileManager: FileManager = .default, boundary: String? = nil)
- fileManager:
FileManager
to use for file operations, if needed. - boundary: Boundary
String
used to separate body parts.
Default memory threshold used when encoding MultipartFormData
, in bytes.
let encodingMemoryThreshold: UInt64 = 10_000_000
The Content-Type
header value containing the boundary used to generate the multipart/form-data
.
var contentType: String = "multipart/form-data; boundary=\(self.boundary)"
The content length of all body parts used to generate the multipart/form-data
not including the boundaries.
var contentLength: UInt64
The boundary used to separate the body parts in the encoded form data.
let boundary: String
Creates a body part from the data and appends it to the instance.
public func append(_ data: Data, withName name: String, fileName: String? = nil, mimeType: String? = nil)
The body part data will be encoded using the following format:
-
Content-Disposition: form-data; name=#{name}; filename=#{filename}
(HTTP Header)
-
Content-Type: #{mimeType}
(HTTP Header)
- data:
Data
to encoding into the instance. - name: Name to associate with the
Data
in theContent-Disposition
HTTP header. - fileName: Filename to associate with the
Data
in theContent-Disposition
HTTP header. - mimeType: MIME type to associate with the data in the
Content-Type
HTTP header.
Creates a body part from the file and appends it to the instance.
public func append(_ fileURL: URL, withName name: String)
The body part data will be encoded using the following format:
-
Content-Disposition: form-data; name=#{name}; filename=#{generated filename}
(HTTP Header)
-
Content-Type: #{generated mimeType}
(HTTP Header)
The filename in the Content-Disposition
HTTP header is generated from the last path component of the
fileURL
. The Content-Type
HTTP header MIME type is generated by mapping the fileURL
extension to the
system associated MIME type.
- fileURL:
URL
of the file whose content will be encoded into the instance. - name: Name to associate with the file content in the
Content-Disposition
HTTP header.
Creates a body part from the file and appends it to the instance.
public func append(_ fileURL: URL, withName name: String, fileName: String, mimeType: String)
The body part data will be encoded using the following format:
- Content-Disposition: form-data; name=#{name}; filename=#{filename} (HTTP Header)
- Content-Type: #{mimeType} (HTTP Header)
- fileURL:
URL
of the file whose content will be encoded into the instance. - name: Name to associate with the file content in the
Content-Disposition
HTTP header. - fileName: Filename to associate with the file content in the
Content-Disposition
HTTP header. - mimeType: MIME type to associate with the file content in the
Content-Type
HTTP header.
Creates a body part from the stream and appends it to the instance.
public func append(_ stream: InputStream, withLength length: UInt64, name: String, fileName: String, mimeType: String)
The body part data will be encoded using the following format:
-
Content-Disposition: form-data; name=#{name}; filename=#{filename}
(HTTP Header)
-
Content-Type: #{mimeType}
(HTTP Header)
- stream:
InputStream
to encode into the instance. - length: Length, in bytes, of the stream.
- name: Name to associate with the stream content in the
Content-Disposition
HTTP header. - fileName: Filename to associate with the stream content in the
Content-Disposition
HTTP header. - mimeType: MIME type to associate with the stream content in the
Content-Type
HTTP header.
Creates a body part with the stream, length, and headers and appends it to the instance.
public func append(_ stream: InputStream, withLength length: UInt64, headers: HTTPHeaders)
The body part data will be encoded using the following format:
- stream:
InputStream
to encode into the instance. - length: Length, in bytes, of the stream.
- headers:
HTTPHeaders
for the body part.
Encodes all appended body parts into a single Data
value.
public func encode() throws -> Data
Note: This method will load all the appended body parts into memory all at the same time. This method should only be used when the encoded data will have a small memory footprint. For large data cases, please use the
writeEncodedData(to:))
method.
An AFError
if encoding encounters an error.
The encoded Data
, if encoding is successful.
Writes all appended body parts to the given file URL
.
public func writeEncodedData(to fileURL: URL) throws
This process is facilitated by reading and writing with input and output streams, respectively. Thus, this approach is very memory efficient and should be used for large body part data.
- Parameter fileURL: File
URL
to which to write the form data.
An AFError
if encoding encounters an error.
Generated at 2020-09-24T22:11:06+0000 using swift-doc 1.0.0-beta.4.
Types
- AF
- AFError
- AFError.MultipartEncodingFailureReason
- AFError.ParameterEncoderFailureReason
- AFError.ParameterEncoderFailureReason.RequiredComponent
- AFError.ParameterEncodingFailureReason
- AFError.ResponseSerializationFailureReason
- AFError.ResponseValidationFailureReason
- AFError.ServerTrustFailureReason
- AFError.ServerTrustFailureReason.Output
- AFError.URLRequestValidationFailureReason
- Adapter
- AlamofireExtension
- AlamofireNotifications
- ClosureEventMonitor
- CompositeEventMonitor
- CompositeTrustEvaluator
- ConnectionLostRetryPolicy
- DataRequest
- DataResponse
- DataResponseSerializer
- DecodableResponseSerializer
- DefaultTrustEvaluator
- DisabledEvaluator
- DownloadRequest
- DownloadRequest.Downloadable
- DownloadRequest.Options
- DownloadResponse
- Empty
- GoogleXSSIPreprocessor
- HTTPHeader
- HTTPHeaders
- HTTPMethod
- Interceptor
- JSONEncoding
- JSONParameterEncoder
- JSONResponseSerializer
- MultipartFormData
- NetworkReachabilityManager
- NetworkReachabilityManager.NetworkReachabilityStatus
- NetworkReachabilityManager.NetworkReachabilityStatus.ConnectionType
- PassthroughPreprocessor
- PinnedCertificatesTrustEvaluator
- PublicKeysTrustEvaluator
- Redirector
- Redirector.Behavior
- Request
- Request.State
- ResponseCacher
- ResponseCacher.Behavior
- Retrier
- RetryPolicy
- RetryResult
- RevocationTrustEvaluator
- RevocationTrustEvaluator.Options
- ServerTrustManager
- Session
- SessionDelegate
- StringResponseSerializer
- URLEncodedFormEncoder
- URLEncodedFormEncoder.ArrayEncoding
- URLEncodedFormEncoder.BoolEncoding
- URLEncodedFormEncoder.DataEncoding
- URLEncodedFormEncoder.DateEncoding
- URLEncodedFormEncoder.Error
- URLEncodedFormEncoder.KeyEncoding
- URLEncodedFormEncoder.SpaceEncoding
- URLEncodedFormParameterEncoder
- URLEncodedFormParameterEncoder.Destination
- URLEncoding
- URLEncoding.ArrayEncoding
- URLEncoding.BoolEncoding
- URLEncoding.Destination
- UploadRequest
- UploadRequest.Uploadable
Protocols
- AlamofireExtended
- CachedResponseHandler
- DataDecoder
- DataPreprocessor
- DataResponseSerializerProtocol
- DownloadResponseSerializerProtocol
- EmptyResponse
- EventMonitor
- ParameterEncoder
- ParameterEncoding
- RedirectHandler
- RequestAdapter
- RequestDelegate
- RequestInterceptor
- RequestRetrier
- ResponseSerializer
- ServerTrustEvaluating
- URLConvertible
- URLRequestConvertible
- UploadConvertible
- UploadableConvertible