iOS通过原生类上传文件至服务器

这里不做多的介绍,直接上代码,有问题加QQ了解409961891

- (void)pause{

[self.uploadTask cancel];

}

- (void)startUploadWithPath:(NSString *)path data:(NSData *)data fileName:(NSString *)fileName{

//1.创建会话对象,设置代理

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

//2.创建请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:path.jq_URL];

//2.1 修改请求方法

request.HTTPMethod = @"POST";

//2.2 设置请求头

NSString *header = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",kBoundary];

[request setValue:header forHTTPHeaderField:@"Content-Type"];

//3.创建上传task

/*

第一个参数:请求对象

第二个参数:要上传文件的参数(二进制数据

第三个参数:completionHandler

data:服务器返回的结果(响应体信息)

response:响应头

*/

self.uploadTask = [session uploadTaskWithRequest:request fromData:[self getBodyDataWithImageData:data fileName:fileName] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

//更新数据库

RLMRealm *realm = [RLMRealm defaultRealm];

[[RLMRealm defaultRealm] beginWriteTransaction];

[UPModel createOrUpdateInRealm:realm withValue:@{@"fileName": self.name, @"finish": @(YES), @"finishTime": [Tool getNowTime], }];

[[RLMRealm defaultRealm] commitWriteTransaction];

//结束定时器

[self.timer invalidate];

//5.解析结果

NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);

//移除通知

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"pauseUpload" object:nil];

}];

//4.执行任务

[self.uploadTask resume];

}

#define kBoundary @"----WebKitFormBoundary0IQAt0HA7oxwIx3f"

#define KNewLine [@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]

-(NSData *)getBodyDataWithImageData:(NSData *)imageData fileName:(NSString *)fileName{

//5.拼接数据

NSMutableData *fileData = [NSMutableData data];

//5.1 拼接文件参数

/*

--分隔符

Content-Disposition: form-data; name="file"; filename="Snip20151228_572.png"

Content-Type: image/png

空行

文件二进制数据

*/

[fileData appendData:[[NSString stringWithFormat:@"--%@",kBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

// name="file":参数,是固定的

// filename:文件上传到服务器以什么名字来保存,随便

[fileData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", fileName] dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

//Content-Type:要上传的文件的类型 (MIMEType)

[fileData appendData: [@"Content-Type: image/png" dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

[fileData appendData:KNewLine];

[fileData appendData:imageData];

[fileData appendData:KNewLine];

//5.2 拼接非文件参数

/*

--分隔符

Content-Disposition: form-data; name="username"

空行

非文件参数的二进制数据

*/

[fileData appendData:[[NSString stringWithFormat:@"--%@",kBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

//username:同file 是服务器规定

[fileData appendData:[@"Content-Disposition: form-data; name=\"username\"" dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

[fileData appendData:KNewLine];

[fileData appendData:[@"dashen9" dataUsingEncoding:NSUTF8StringEncoding]];

[fileData appendData:KNewLine];

//5.3 拼接结尾标识

/*

--分隔符--

*/

[fileData appendData:[[NSString stringWithFormat:@"--%@--",kBoundary] dataUsingEncoding:NSUTF8StringEncoding]];

return fileData;

}

你可能感兴趣的:(iOS通过原生类上传文件至服务器)