#iOS AF上传图片引发的血案

AF带来的血案

#iOS AF上传图片引发的血案_第1张图片
#iOS AF上传图片引发的血案_第2张图片
form-data.png

已上图是AF源码截图,基于表单的形式上传。使用的是multipart/form-data发送文件。
Content-Type必须是multipart/form-data

原理分析

以multipart/form-data编码的POST请求格式与application/x-www-form-urlencoded完全不同.

其中application/x-www-form-urlencoded提交的数据按照 key1=val1&key2=val2 的方式进行编码,key 和 val 都进行了 URL 转码。然而multipart/form-data是这样的形式:

key1 = val1

key2 = vals

关于 multipart/form-data 的详细定义,请前往 rfc1867 查看。http://www.ietf.org/rfc/rfc1867.txt

例子


POST http://www.example.com HTTP/1.1
Content-Type:multipart/form-data; boundary=XXX
此处有有空格*
--XXX
Content-Disposition: form-data; name="text"

title
--XXX
Content-Disposition: form-data; name="file"; filename="chrome.png"
Content-Type: image/png
此处有有空格*
PNG ... content of chrome.png ...
--XXX

例子说明

  • 首先生成了一个 boundary=XXX 用于分割不同的字段,为了避免与正文内容重复,boundary 可能会很长很复杂。然后 Content-Type 里指明了数据是以 multipart/form-data 来编码。

  • 消息主体里按照字段个数又分为多个结构类似的部分,每部分都是以 --boundary 开始,紧接着是内容描述信息,然后是回车,最后是字段具体内容(文本或二进制)。

  • 如果传输的是文件,还要包含文件名和文件类型信息。

  • 消息主体最后以 --boundary-- 标示结束。

iOS多图片上传

/**

  • 上传图片
  • @param operations 上传图片等预留参数---视具体情况而定 可移除
  • @param imageArray 上传的图片数组
  • @parm width 图片要被压缩到的宽度
  • @param urlString 上传的url---请填写完整的url
  • @param successBlock 上传成功的回调
  • @param failureBlock 上传失败的回调
  • @param progress 上传进度

*/
+(void)uploadImageWithOperations:(NSDictionary *)operations withImageArray:(NSArray *)imageArray withtargetWidth:(CGFloat )width withUrlString:(NSString *)urlString withSuccessBlock:(requestSuccess)successBlock withFailurBlock:(requestFailure)failureBlock withUpLoadProgress:(uploadProgress)progress;
{

//1.创建管理者对象

  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

 [manager POST:urlString parameters:operations constructingBodyWithBlock:^(id  _Nonnull formData) {
    
    NSUInteger i = 0 ;
    
    /**出于性能考虑,将上传图片进行压缩*/
    for (UIImage * image in imageArray) {
        
        //image的分类方法
        UIImage *  resizedImage =  [UIImage IMGCompressed:image targetWidth:width];
        
        NSData * imgData = UIImageJPEGRepresentation(resizedImage, .5);
        
        //拼接data
        [formData appendPartWithFileData:imgData name:[NSString stringWithFormat:@"picflie%ld",(long)i] fileName:@"image.png" mimeType:@" image/jpeg"];
        
        i++;
    }
    
} progress:^(NSProgress * _Nonnull uploadProgress) {
    
    progress(uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    
} success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
    
    successBlock(responseObject);
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
   
    failureBlock(error);
    
}];

}

扩展

四种常见的 POST 提交数据方式:以上两种 + application/json + text/xml

参考

1.http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data

2.https://imququ.com/post/four-ways-to-post-data-in-http.html#toc-2

你可能感兴趣的:(#iOS AF上传图片引发的血案)