上傳文件格式
POST /php/upload/upload.php HTTP/1.1
Host: 127.0.0.1
Content-Type: multipart/form-data; boundary=標識(可自定義)
請求體
--標識(可自定義,但必須與請求頭中一致)
Content-Disposition: form-data; name="userfile[]"; filename="head1.png"
Content-Type: image/png
空行
文件二進制數據
--標識(可自定義,但必須與請求頭中一致)
Content-Disposition: form-data; name="userfile[]"; filename="head2.png"
Content-Type: image/png
空行
文件二進制數據
--標識(可自定義,但必須與請求頭中一致)
Content-Disposition: form-data; name="字段名"
數據值
--標識(可自定義,但必須與請求頭中一致)--
多文件文件上傳
#define kBOUNDARY @"abc"
- (void)viewDidLoad {
[super viewDidLoad];
// 網絡鏈接
NSString *netUrl = @"http://127.0.0.1/php/upload/upload-m.php";
// 文件路徑
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"head1.png" ofType:nil];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"head2.png" ofType:nil];
NSArray *array = @[path1, path2];
// 字段名
NSString *fieldName = @"userfile[]";
// 數據字典
NSDictionary *dict = @{@"username":@"mazaiting"};
// 上傳文件
[self uploadFiles:netUrl fieldName:fieldName filePaths:array params:dict];
}
// 上傳多個文件
// netUrl 網絡鏈接
// fieldName 字段名
// filePaths 文件路徑數組
// params 參數字典
- (void)uploadFiles:(NSString *)netUrl fieldName:(NSString *)fieldName filePaths:(NSArray *)filePaths params:(NSDictionary *)params {
NSURL *url = [NSURL URLWithString:netUrl];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"post";
// Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryJa8BALfIc9saou2X
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBOUNDARY] forHTTPHeaderField:@"Content-Type"];
request.HTTPBody = [self body:fieldName filePaths:filePaths params:params];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:
^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"連接錯誤 %@", connectionError);
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200 || httpResponse.statusCode == 304) {
// 解析數據
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",dict);
} else {
NSLog(@"服務器內部錯誤");
}
}];
}
// 構建請求體
- (NSData *)body:(NSString *)fieldName filePaths:(NSArray *)filePaths params:(NSDictionary *)params {
NSMutableData *mData = [NSMutableData data];
// ------WebKitFormBoundaryJa8BALfIc9saou2X
// Content-Disposition: form-data; name="userfile[]"; filename="head1.png"
// Content-Type: image/png
//
// 文件二進制數據
// ------WebKitFormBoundaryJa8BALfIc9saou2X
// Content-Disposition: form-data; name="userfile[]"; filename="head2.png"
// Content-Type: image/png
//
// 文件二進制數據
// ------WebKitFormBoundaryJa8BALfIc9saou2X
// Content-Disposition: form-data; name="username"
//
// mazaiting
// ------WebKitFormBoundaryJa8BALfIc9saou2X--
// 構建文件,遍歷數組
[filePaths enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
// ------WebKitFormBoundaryJa8BALfIc9saou2X
// Content-Disposition: form-data; name="userfile[]"; filename="head2.png"
// Content-Type: image/png
//
// 文件二進制數據
NSMutableString *mString = [NSMutableString string];
// 判斷是否是第一個文件,如果是則不需要添加"\r\n"
if (idx != 0) {
[mString appendString:@"\r\n"];
}
[mString appendFormat:@"--%@\r\n", kBOUNDARY];
[mString appendFormat:@"Content-Disposition: form-data; name=%@; filename=%@\r\n", fieldName, [obj lastPathComponent]];
[mString appendString:@"Content-Type: application/octet-stream\r\n"];
[mString appendString:@"\r\n"];
[mData appendData:[mString dataUsingEncoding:NSUTF8StringEncoding]];
// 拼接文件的二進制數據
NSData *data = [NSData dataWithContentsOfFile:obj];
[mData appendData:data];
}];
// 構建數據
// ------WebKitFormBoundaryJa8BALfIc9saou2X
// Content-Disposition: form-data; name="username"
//
// mazaiting
// ------WebKitFormBoundaryJa8BALfIc9saou2X--
[params enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSMutableString *mString = [NSMutableString string];
[mString appendFormat:@"\r\n--%@\r\n", kBOUNDARY];
[mString appendFormat:@"Content-Disposition: form-data; name=%@\r\n", key];
[mString appendString:@"\r\n"];
[mString appendFormat:@"%@", obj];
[mData appendData:[mString dataUsingEncoding:NSUTF8StringEncoding]];
}];
// 結束語句
NSString *end = [NSString stringWithFormat:@"\r\n--%@--", kBOUNDARY];
[mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
return mData.copy;
}