ASIFormDataRequest ASIMultipartFormDataPostFormat标头格式

时间:2013-04-24 08:35:13

标签: cocoa-touch http http-headers multipartform-data asiformdatarequest

我对多部分邮件的标题格式有问题。如何为一个部件创建一个包含多个值的标头。

我想要的是什么:

--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
OBR_SORT_IDX:1
OBR_ENUM:1
Content-Disposition: form-data; name="IMAGE"; filename="image1_0.png"
Content-Type: image/png

<theBinData>

我有什么:

--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="OBR_ENUM"

1
--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="OBR_SORT_IDX"

1
--0xKhTmLbOuNdArY-28217FDC-8163-459B-AB09-3DE87210721D
Content-Disposition: form-data; name="IMAGE"; filename="image1_0.png"
Content-Type: image/png

<theBinData>

我用于测试的代码:

UIImage *img1 = [UIImage imageNamed:@"alu_texture.png"];
UIImage *img2 = [UIImage imageNamed:@"alu_texture.png"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strUrl]];
[request setPostBodyFilePath:thePath];
request.shouldStreamPostDataFromDisk = YES;

[request setPostFormat:ASIMultipartFormDataPostFormat];
[request setRequestMethod:@"POST"];

NSString *imgName = [NSString stringWithFormat:@"image%d_%d.png", 1, 0];
NSData *data = [NSData dataWithData: UIImagePNGRepresentation(img1)];
[request addPostValue:@"1" forKey:@"OBR_ENUM"];
[request addPostValue:@"0" forKey:@"OBR_SORT_IDX"];
[request addData:data withFileName:imgName andContentType:@"image/png" forKey:@"IMAGE"];

imgName = [NSString stringWithFormat:@"image%d_%d.png", 1, 1];
data = [NSData dataWithData: UIImagePNGRepresentation(img2)];
[request addPostValue:@"1" forKey:@"OBR_ENUM"];
[request addPostValue:@"1" forKey:@"OBR_SORT_IDX"];
[request addData:data withFileName:imgName andContentType:@"image/png" forKey:@"IMAGE"];

[request setDelegate:self];
[request startAsynchronous]

1 个答案:

答案 0 :(得分:0)

所以我编辑了- (void)buildMultipartFormDataPostBody的方法ASIFormDataRequest。我为multipart标题添加了带字典的新数组。

buildMultipartFormDataPostBody的for循环中

代码fileData

NSDictionary *d = [self headerMultyPartData][i];
NSArray *keys = d.allKeys;
for(NSString *key in keys)
    [self appendPostString:[NSString stringWithFormat:@"%@: %@\r\n", key, [d valueForKey:key]]];