程序员@IT·互联网iOS Developer

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

2016-08-14  本文已影响966人  践行者

AF带来的血案

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

例子

<pre>
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

</pre>

例子说明

iOS多图片上传

<pre>

/**

*/
+(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<AFMultipartFormData>  _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);
    
}];

}

</pre>

扩展

四种常见的 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

上一篇下一篇

猜你喜欢

热点阅读