由于iOS无法通过html表单来上传图片,因此想要上传图片,必须实现http请求,而不能像其他语言那样通过html表单的post就能上传。
上传图片的http post请求的格式是这样的:
Java代码
- 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--
第一行是指定了http post请求的编码方式为multipart/form-data(上传文件必须用这个)。
boundary=AaB03x说明了AaB03x为分界线。比如 --AaB03x 就是一个分界线的意思
content-disposition: form-data; name="field1"
Hello Boris!
这句话声明了请求中的一个字段的名称,如field1 以及字段的值,如Hello Boris!
这里类似form表单中的<input name="field1" type="text" value="Hello Boris!"/>
中间的空行是必须的。
不同的字段之间用分界线分开,分界线需要单独一行,如 --AaB03x--
分界线的下一行,是下一个字段
content-disposition: form-data; name="pic"; filename="boris.png"
Content-Type: image/png
... contents of boris.png ...
--AaB03x--
这里声明了变量pic,也就是我们要传的文件,上传文件的时候需要在后边指定file name:filename="boris.png"
并且需要在下一行指定文件的格式:Content-Type: image/png
... contents of boris.png ... 这里是boris.png的二进制内容,如 <89504e47 0d0a1a0a 0000000d 49484452 000000b4 000000b4 08020000 00b2af91 65000020 00494441 5478012c
dd79b724 6b7616f6 8c888c88 8c9c8733 55ddb1d5 6a0db486 06218401 ......
在http post请求的结尾,需要有一个分界线,但是是前后都有--的:--AaB03x--
以上的这些格式,是http的规范,每个空行,空格都是必须的。
下边是iOS的实现代码:
#define HTTP_CONTENT_BOUNDARY @"WANPUSH" -(BOOL)httpPutData:(NSString*)strUrl FilePath:(NSString*)filePath DataType:(NSString*)dataType { strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL* url = [NSURL URLWithString:strUrl]; NSData* data = [NSData dataWithContentsOfFile:filePath]; NSString* fileName = [filePath lastPathComponent]; NSString* strBodyBegin = [NSString stringWithFormat:@"--%@\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\"\nContent-Type: %@\n\n", HTTP_CONTENT_BOUNDARY, @"file", fileName, dataType]; NSString* strBodyEnd = [NSString stringWithFormat:@"\n--%@--",HTTP_CONTENT_BOUNDARY]; NSMutableData *httpBody = [NSMutableData data]; [httpBody appendData:[strBodyBegin dataUsingEncoding:NSUTF8StringEncoding]]; [httpBody appendData:data]; [httpBody appendData:[strBodyEnd dataUsingEncoding:NSUTF8StringEncoding]]; NSMutableURLRequest* httpPutRequest = [[NSMutableURLRequest alloc] init]; [httpPutRequest setURL:url]; [httpPutRequest setHTTPMethod:@"POST"]; [httpPutRequest setTimeoutInterval: 60000]; [httpPutRequest setValue:[NSString stringWithFormat:@"%@", @(httpBody.length)] forHTTPHeaderField:@"Content-Length"]; [httpPutRequest setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",HTTP_CONTENT_BOUNDARY] forHTTPHeaderField:@"Content-Type"]; httpPutRequest.HTTPBody = httpBody; NSHTTPURLResponse* httpResponse = nil; NSError *error = [[NSError alloc] init]; NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&httpResponse error:&error]; if (httpResponse == nil) { NSLog(@"url: %@\nerror_code: %@", strUrl, error); return NO; } if (httpResponse.statusCode != 200) { NSLog(@"url: %@\nHTTP response: %ld", strUrl, (long)httpResponse.statusCode); return NO; } return YES; }
后台php代码:
<?php $target_path = "./tmp/";// $target_path = $target_path.($_FILES['file']['name']); $target_path = iconv("UTF-8","gb2312", $target_path); if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) { echo "The file ".( $_FILES['file']['name'])." has been uploaded."; }else{ echo "There was an error uploading the file, please try again! Error Code: ".$_FILES['file']['name']; } ?>