Reprinted from: http://www.cocoachina.com/bbs/read.php?tid=89985 since iOS cannot upload images via HTML forms, it is necessary to implement HTTP requests to upload images. Instead of being able to upload it through the post of an HTML form like any other language.
The format of the HTTP POST request for uploading images is this:
?
123456789101112 |
Content-type: multipart/form-data, boundary=AaB03x --AaB03x content-disposition: form-data; name= "field1" Hello Boris! --AaB03x content-disposition: form-data; name= "pic" ; filename= "boris.png" Content-Type: image/png ... contents of boris.png ... --AaB03x-- |
The first line specifies that the HTTP POST request is encoded as multipart/form-data (this must be used for uploading the file).
Boundary=aab03x explained that aab03x was the dividing line. Like--aab03x is the meaning of a dividing line.
Content-disposition:form-data; Name= "Field1"
Hello boris!
This statement declares the name of a field in the request, such as field1 and the value of the field, such as Hello boris!
This is similar to the <input name= "field1" type= "text" value= "Hello boris!" in form form />
A blank line in the middle is required.
Separate fields are separated by dividing lines, which require a separate line, such as--aab03x--
The next line of demarcation is the next field
Content-disposition:form-data; Name= "pic"; Filename= "Boris.png"
Content-type:image/png
... contents of boris.png ...
--aab03x--
Here we declare the variable pic, which is the file we want to pass, and we need to specify file Name:filename= "Boris.png" when uploading files.
And you need to specify the format of the file on the following line: Content-type:image/png
... contents of boris.png ... Here is boris.png binary content, such as <89504e47 0d0a1a0a 0000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 547801 2c dd79b724 6b7616f6 8c888c88 8c9c8733 55ddb1d5 6a0db486 06218401 ...
At the end of the HTTP POST request, there needs to be a dividing line, but there are both before and after:--aab03x--
The above formats are HTTP specifications, each blank line, and spaces are required.
Below is the implementation code for iOS:
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172 |
//分界线的标识符
NSString *TWITTERFON_FORM_BOUNDARY = @
"AaB03x"
;
//根据url初始化request
NSMutableURLRequest
* request = [
NSMutableURLRequest requestWithURL:[
NSURL URLWithString:url]
cachePolicy:
NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
//分界线 --AaB03x
NSString *MPboundary=[[
NSString alloc]initWithFormat:@
"--%@"
,TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[
NSString alloc]initWithFormat:@
"%@--"
,MPboundary];
//要上传的图片
UIImage *image=[params objectForKey:@
"pic"
];
//得到图片的data
NSData
* data = UIImagePNGRepresentation(image);
//http body的字符串
NSMutableString *body=[[
NSMutableString alloc]init];
//参数的集合的所有key的集合
NSArray *keys= [params allKeys];
//遍历keys
for
(
int i=0;i<[keys count];i++)
{
//得到当前key
NSString *key=[keys objectAtIndex:i];
//如果key不是pic,说明value是字符类型,比如name:Boris
if
(![key isEqualToString:@
"pic"
])
{
//添加分界线,换行
[body appendFormat:@
"%@\r\n"
,MPboundary];
//添加字段名称,换2行
[body appendFormat:@
"Content-Disposition: form-data; name=\"%@\"\r\n\r\n"
,key];
//添加字段的值
[body appendFormat:@
"%@\r\n"
,[params objectForKey:key]];
}
}
////添加分界线,换行
[body appendFormat:@
"%@\r\n"
,MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@
"Content-Disposition: form-data; name=\"pic\"; filename=\"boris.png\"\r\n"
];
//声明上传文件的格式
[body appendFormat:@
"Content-Type: image/png\r\n\r\n"
];
//声明结束符:--AaB03x--
NSString *
end
=[[
NSString alloc]initWithFormat:@
"\r\n%@"
,endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[
NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:
NSUTF8StringEncoding
]];
//将image的data加入
[myRequestData appendData:data];
//加入结束符--AaB03x--
[myRequestData appendData:[
end dataUsingEncoding:
NSUTF8StringEncoding
]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[
NSString alloc]initWithFormat:@
"multipart/form-data; boundary=%@"
,TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@
"Content-Type"
];
//设置Content-Length
[request setValue:[
NSString stringWithFormat:@
"%d"
, [myRequestData length]] forHTTPHeaderField:@
"Content-Length"
];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@
"POST"
];
//建立连接,设置代理
NSURLConnection *conn = [[
NSURLConnection alloc] initWithRequest:request delegate:
self
];
//设置接受response的data
if (conn) {
mResponseData = [[
NSMutableData data] retain];
|
iOS uploads images via HTTP post (GO)