原生代码上传文件

上传文件

-(void)uploadFileWithURL:(NSString *)urlString params:(NSDictionary *)params fileKey:(NSString *)fileKey filePath:(NSString *)filePath completeHander:(void (^)(NSURLResponse *, NSData *, NSError *))completeHander{

NSURL *URL = [[NSURL alloc]  initWithString:urlString];

request = [[NSMutableURLRequest alloc]  initWithURL:URL cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];

NSString *boundary = @"wfWiEWrgEFA9A78512weF7106A";

NSData *fileData = [NSData dataWithContentsOfFile:filePath];

request.HTTPMethod = @"POST";

request.allHTTPHeaderFields = @{

@"Content-Type":[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]

};

//multipart/form-data格式按照构建上传数据

NSMutableData *postData = [[NSMutableData alloc]init];

for (NSString *key in params) {

NSString *pair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n",boundary,key];

[postData appendData:[pair dataUsingEncoding:NSUTF8StringEncoding]];

id value = [params objectForKey:key];

if ([value isKindOfClass:[NSString class]]) {

[postData appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];

}else if ([value isKindOfClass:[NSData class]]){

[postData appendData:value];

}

[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

}

//文件部分

NSString *filename = [filePath lastPathComponent];

NSString *contentType = AFContentTypeForPathExtension([filePath pathExtension]);

NSString *filePair = [NSString stringWithFormat:@"--%@\r\nContent-Disposition: form-data; name=\"%@\"; filename=\"%@\";Content-Type=%@\r\n\r\n",boundary,fileKey,filename,contentType];

[postData appendData:[filePair dataUsingEncoding:NSUTF8StringEncoding]];

//[postData appendData:[@"测试文件数据" dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:fileData]; //加入文件的数据

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

request.HTTPBody = postData;

[request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"];

_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[_connection start];

}

获得文件类型

#import

static inline NSString * AFContentTypeForPathExtension(NSString *extension) {

#ifdef __UTTYPE__

NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);

NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);

if (!contentType) {

return @"application/octet-stream";

} else {

return contentType;

}

#else

#pragma unused (extension)

return @"application/octet-stream";

#endif

}

你可能感兴趣的:(原生代码上传文件)