A.文件上傳
思路:
發(fā)送文件數(shù)據(jù)給服務(wù)器
使用post請求
必須手動設(shè)置請求頭: 內(nèi)容大小Content-Length & 內(nèi)容類型 Content-Type
請求體:文件數(shù)據(jù)
文件上傳的格式要求十分嚴格,必須嚴格遵守
由于是一次性加載文件到內(nèi)存上傳,所以只能用于小文件上傳
B.實現(xiàn)
1.設(shè)置POST請求
(1)使用POST請求方法
(2)設(shè)置請求頭
設(shè)置內(nèi)容長度、內(nèi)容類型、分割線
(3)設(shè)置請求體
NSMutableData *body = [NSMutableData data];
分割線 + 換行
內(nèi)容描述 + 換行
內(nèi)容類型 + 換行
換行
文件二進制數(shù)據(jù) + 換行
分割線–
multipart/form-data 中的內(nèi)容
Image(46)
例如使用chrome上傳一張圖片:
5A66D35E-E958-49A9-AD92-9C54E4CCFA83
(4)使用本地請求獲取某種文件類型的MIMEType
/** 取得本地文件的MIMEType */
- (void) getMIMEType {
// Socket 實現(xiàn)斷點上傳
//apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
// UIImage *image = [UIImage imageNamed:@"test"];
// NSData *filedata = UIImagePNGRepresentation(image);
// [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
// 給本地文件發(fā)送一個請求
NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
NSURLResponse *repsonse = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
// 得到mimeType
NSLog(@"%@", repsonse.MIMEType);
[self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}];
附:常見文件類型的MIMEType
Image(47)
ViewController.m
UploadFileDemo
#import "ViewController.h"
#define UTF8Encode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@interface ViewController ()
- (IBAction)upload;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated. }
- (IBAction)upload {
UIImage *image = [UIImage imageNamed:@"IMG_0413"];
NSData *imageData = UIImagePNGRepresentation(image);
[self upload:@"uploadedFile" filename:@"IMG_0413.PNG" mimeType:@"image/png" data:imageData parmas:nil];
}
- (void)upload:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType data:(NSData *)data parmas:(NSDictionary *)params
{
// 文件上傳
NSURL *url = [NSURL URLWithString:@"http://192.168.0.21:8080/MyTestServer/upload"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 設(shè)置請求體
NSMutableData *body = [NSMutableData data];
/***************文件參數(shù)***************/
// 參數(shù)開始的標志
[body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")];
// name : 指定參數(shù)名(必須跟服務(wù)器端保持一致)
// filename : 文件名
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n", name, filename];
[body appendData:UTF8Encode(disposition)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@\r\n", mimeType];
[body appendData:UTF8Encode(type)];
[body appendData:UTF8Encode(@"\r\n")];
[body appendData:data];
[body appendData:UTF8Encode(@"\r\n")];
/***************普通參數(shù)***************/
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { // 參數(shù)開始的標志
[body appendData:UTF8Encode(@"--HelloVoidWorldBoundary\r\n")];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n", key];
[body appendData:UTF8Encode(disposition)];
[body appendData:UTF8Encode(@"\r\n")];
[body appendData:UTF8Encode(obj)];
[body appendData:UTF8Encode(@"\r\n")];
}];
/***************參數(shù)結(jié)束***************/
// HelloVoidWorldBoundary--\r\n
[body appendData:UTF8Encode(@"--HelloVoidWorldBoundary--\r\n")];
request.HTTPBody = body;
// 設(shè)置請求頭
// 請求體的長度
[request setValue:[NSString stringWithFormat:@"%zd", body.length] forHTTPHeaderField:@"Content-Length"];
// 聲明這個POST請求是個文件上傳
[request setValue:@"multipart/form-data; boundary=HelloVoidWorldBoundary" forHTTPHeaderField:@"Content-Type"];
// 發(fā)送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"開始上傳~~~");
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSLog(@"%@", dict); } else {
NSLog(@"上傳失敗"); }
}];
}
/** 取得本地文件的MIMEType */
- (void) getMIMEType {
//apache-tomcat-6.0.41/conf/web.xml 查找 文件的 mimeType
// UIImage *image = [UIImage imageNamed:@"test"];
// NSData *filedata = UIImagePNGRepresentation(image);
// [self upload:@"file" filename:@"test.png" mimeType:@"image/png" data:filedata parmas:@{@"username" : @"123"}];
// 給本地文件發(fā)送一個請求
NSURL *fileurl = [[NSBundle mainBundle] URLForResource:@"itcast.txt" withExtension:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:fileurl];
NSURLResponse *repsonse = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&repsonse error:nil];
// 得到mimeType
NSLog(@"%@", repsonse.MIMEType);
[self upload:@"file" filename:@"itcast.txt" mimeType:repsonse.MIMEType data:data parmas:@{@"username":@"tom", @"type":@"xml"}]; }
IOS 多線程 & 網(wǎng)絡(luò)-小文件上傳
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 轉(zhuǎn)載:[IOS 多線程 & 網(wǎng)絡(luò) - 2.5] – 小文件上傳 A.文件上傳 思路: 發(fā)送文件數(shù)據(jù)給服務(wù)器 使用p...
- 前言 1.網(wǎng)絡(luò)請求 系統(tǒng)請求的方法 分為get請求和post請求1.1、get請求系統(tǒng)提供了兩種方法,一種是Req...