LCMultipartFormData
Objective-C
@protocol LCMultipartFormData
Swift
protocol LCMultipartFormData
The LCMultipartFormData protocol defines the methods supported by the parameter in the block argument of LCHTTPRequestSerializer -multipartFormRequestWithMethod:URLString:parameters:constructingBodyWithBlock:.
-
Appends the HTTP header
Content-Disposition: file; filename=#{generated filename}; name=#{name}"andContent-Type: #{generated mimeType}, followed by the encoded file data and the multipart form boundary.The filename and MIME type for this data in the form will be automatically generated, using the last path component of the
fileURLand system associated MIME type for thefileURLextension, respectively.Declaration
Objective-C
- (BOOL)appendPartWithFileURL:(nonnull NSURL *)fileURL name:(nonnull NSString *)name error:(NSError *_Nullable *_Nullable)error;Swift
func appendPart(withFileURL fileURL: URL, name: String) throwsParameters
fileURLThe URL corresponding to the file whose content will be appended to the form. This parameter must not be
nil.nameThe name to be associated with the specified data. This parameter must not be
nil.errorIf an error occurs, upon return contains an
NSErrorobject that describes the problem.Return Value
YESif the file data was successfully appended, otherwiseNO. -
Appends the HTTP header
Content-Disposition: file; filename=#{filename}; name=#{name}"andContent-Type: #{mimeType}, followed by the encoded file data and the multipart form boundary.Declaration
Objective-C
- (BOOL)appendPartWithFileURL:(nonnull NSURL *)fileURL name:(nonnull NSString *)name fileName:(nonnull NSString *)fileName mimeType:(nonnull NSString *)mimeType error:(NSError *_Nullable *_Nullable)error;Swift
func appendPart(withFileURL fileURL: URL, name: String, fileName: String, mimeType: String) throwsParameters
fileURLThe URL corresponding to the file whose content will be appended to the form. This parameter must not be
nil.nameThe name to be associated with the specified data. This parameter must not be
nil.fileNameThe file name to be used in the
Content-Dispositionheader. This parameter must not benil.mimeTypeThe declared MIME type of the file data. This parameter must not be
nil.errorIf an error occurs, upon return contains an
NSErrorobject that describes the problem.Return Value
YESif the file data was successfully appended otherwiseNO. -
Appends the HTTP header
Content-Disposition: file; filename=#{filename}; name=#{name}"andContent-Type: #{mimeType}, followed by the data from the input stream and the multipart form boundary.Declaration
Objective-C
- (void)appendPartWithInputStream:(nullable NSInputStream *)inputStream name:(nonnull NSString *)name fileName:(nonnull NSString *)fileName length:(int64_t)length mimeType:(nonnull NSString *)mimeType;Swift
func appendPart(with inputStream: InputStream?, name: String, fileName: String, length: Int64, mimeType: String)Parameters
inputStreamThe input stream to be appended to the form data
nameThe name to be associated with the specified input stream. This parameter must not be
nil.fileNameThe filename to be associated with the specified input stream. This parameter must not be
nil.lengthThe length of the specified input stream in bytes.
mimeTypeThe MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be
nil. -
Appends the HTTP header
Content-Disposition: file; filename=#{filename}; name=#{name}"andContent-Type: #{mimeType}, followed by the encoded file data and the multipart form boundary.Declaration
Objective-C
- (void)appendPartWithFileData:(nonnull NSData *)data name:(nonnull NSString *)name fileName:(nonnull NSString *)fileName mimeType:(nonnull NSString *)mimeType;Swift
func appendPart(withFileData data: Data, name: String, fileName: String, mimeType: String)Parameters
dataThe data to be encoded and appended to the form data.
nameThe name to be associated with the specified data. This parameter must not be
nil.fileNameThe filename to be associated with the specified data. This parameter must not be
nil.mimeTypeThe MIME type of the specified data. (For example, the MIME type for a JPEG image is image/jpeg.) For a list of valid MIME types, see http://www.iana.org/assignments/media-types/. This parameter must not be
nil. -
Appends the HTTP headers
Content-Disposition: form-data; name=#{name}", followed by the encoded data and the multipart form boundary.Declaration
Objective-C
- (void)appendPartWithFormData:(nonnull NSData *)data name:(nonnull NSString *)name;Swift
func appendPart(withForm data: Data, name: String)Parameters
dataThe data to be encoded and appended to the form data.
nameThe name to be associated with the specified data. This parameter must not be
nil. -
Appends HTTP headers, followed by the encoded data and the multipart form boundary.
Declaration
Objective-C
- (void)appendPartWithHeaders: (nullable NSDictionary<NSString *, NSString *> *)headers body:(nonnull NSData *)body;Swift
func appendPart(withHeaders headers: [String : String]?, body: Data)Parameters
headersThe HTTP headers to be appended to the form data.
bodyThe data to be encoded and appended to the form data. This parameter must not be
nil. -
Throttles request bandwidth by limiting the packet size and adding a delay for each chunk read from the upload stream.
When uploading over a 3G or EDGE connection, requests may fail with “request body stream exhausted”. Setting a maximum packet size and delay according to the recommended values (
kLCUploadStream3GSuggestedPacketSizeandkLCUploadStream3GSuggestedDelay) lowers the risk of the input stream exceeding its allocated bandwidth. Unfortunately, there is no definite way to distinguish between a 3G, EDGE, or LTE connection overNSURLConnection. As such, it is not recommended that you throttle bandwidth based solely on network reachability. Instead, you should consider checking for the “request body stream exhausted” in a failure block, and then retrying the request with throttled bandwidth.Declaration
Objective-C
- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes delay:(NSTimeInterval)delay;Swift
func throttleBandwidth(withPacketSize numberOfBytes: UInt, delay: TimeInterval)Parameters
numberOfBytesMaximum packet size, in number of bytes. The default packet size for an input stream is 16kb.
delayDuration of delay each time a packet is read. By default, no delay is set.
View on GitHub
Install in Dash
LCMultipartFormData Protocol Reference