前言
1、之前项目中有用到表单上传图片、视频等, 从百度、谷歌搜索了好多方法和例子, 借鉴其中,总结了一个更易用的上传文件类。
2、本类没有使用RAC、AFN等第三方,使用的是iOS系统的代理、及上传函数及回调,所以可移植性较强,在此特别记录一下,能帮助到其他有困惑的人就再好不过了!
3、没有额外注释说明,所有的注释均在代码中体现,可自行体会。
LSUpLoadHelper.h 文件的实现
//
// LSUpLoadHelper.h
// RenCheRen
//
// Created by 王隆帅 on 15/8/19.
// Copyright (c) 2015年 王隆帅. All rights reserved.
//
#import <Foundation/Foundation.h>
@class LSUpLoadHelper;
@protocol LSUpLoadHelperDelegate <NSObject>
/**
* 上传进度
*
* @param uploadHelper 本类的实例化
* @param progress 上传进度
*/
- (void)ls_upLoad:(LSUpLoadHelper *)uploadHelper progress:(CGFloat)progress;
/**
* 上传成功回调
*
* @param uploadHelper 本类的实例化
* @param dict 成功返回字典
*/
- (void)ls_upLoad:(LSUpLoadHelper *)uploadHelper finishedWithDict:(NSDictionary *)dict;
/**
* 上传失败回调
*
* @param uploadHelper 本类的实例化
* @param error 错误信息
*/
- (void)ls_upLoad:(LSUpLoadHelper *)uploadHelper failedWithError:(NSError *)error;
@end
@interface LSUpLoadHelper : NSObject <NSURLConnectionDataDelegate>
@property (weak, nonatomic) id <LSUpLoadHelperDelegate> delegate;
/**
* 根据文件路径获取文件的MIMEType
*
* @param url 文件路径
*
* @return 文件MIMEType
*/
- (NSString *)MIMEType:(NSURL *)url;
/**
* 根据文件名、MIMEType、二进制文件、其他的参数上传文件
*
* @param filename 文件名
* @param mimeType MIMEType
* @param fileData 二进制文件
* @param params 非文件的其他详细参数
*/
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params;
@end
LSUpLoadHelper.m文件的实现
//
// LSUpLoadHelper.m
// RenCheRen
//
// Created by 王隆帅 on 15/8/19.
// Copyright (c) 2015年 王隆帅. All rights reserved.
//
#import "LSUpLoadHelper.h"
#define MJFileBoundary @"MalJob"
#define MJNewLine @"\r\n"
#define MJEncode(str) [str dataUsingEncoding:NSUTF8StringEncoding]
@implementation LSUpLoadHelper
- (NSString *)MIMEType:(NSURL *)url {
// 1.创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 2.发送请求(返回响应)
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
// 3.获得MIMEType
return response.MIMEType;
}
- (void)upload:(NSString *)filename mimeType:(NSString *)mimeType fileData:(NSData *)fileData params:(NSDictionary *)params {
// 1.请求路径
NSURL *url = [NSURL URLWithString:REQUEST_URL];
// 2.创建一个POST请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 3.设置请求体
NSMutableData *body = [NSMutableData data];
// 3.1.文件参数
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(MJNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"", filename];
[body appendData:MJEncode(disposition)];
[body appendData:MJEncode(MJNewLine)];
NSString *type = [NSString stringWithFormat:@"Content-Type: %@", mimeType];
[body appendData:MJEncode(type)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:fileData];
[body appendData:MJEncode(MJNewLine)];
// 3.2.非文件参数
[params enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(MJNewLine)];
NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", key];
[body appendData:MJEncode(disposition)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode(MJNewLine)];
[body appendData:MJEncode([obj description])];
[body appendData:MJEncode(MJNewLine)];
}];
// 3.3.结束标记
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJFileBoundary)];
[body appendData:MJEncode(@"--")];
[body appendData:MJEncode(MJNewLine)];
request.HTTPBody = body;
// 4.设置请求头(告诉服务器这次传给你的是文件数据,告诉服务器现在发送的是一个文件上传请求)
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MJFileBoundary];
[request setValue:contentType forHTTPHeaderField:@"Content-Type"];
// 5.发送请求
// 实例方法 设置代理 并获取 进度
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
// 类方法 上传文件 但是 没有代理 不能记录进度
// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//
// NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves er ror:nil];
// NSLog(@"%@", dict);
//
// }];
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
if (self.delegate && [self.delegate respondsToSelector:@selector(ls_upLoad:progress:)]) {
[self.delegate ls_upLoad:self progress:1 - (CGFloat)bytesWritten/(CGFloat)totalBytesWritten];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (self.delegate && [self.delegate respondsToSelector:@selector(ls_upLoad:failedWithError:)]) {
[self.delegate ls_upLoad:self failedWithError:error];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if (self.delegate && [self.delegate respondsToSelector:@selector(ls_upLoad:finishedWithDict:)]) {
[self.delegate ls_upLoad:self finishedWithDict:dict];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"上传完成后续处理");
}
#pragma mark - 请求示例
- (void)upload {
// 非文件的其他详细参数
NSDictionary *params = @{
@"" : @"",
@"" : @"",
};
NSURL *url = [[NSBundle mainBundle] URLForResource:@"filename" withExtension:@"txt"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *MIMEType = [self MIMEType:url];
[self upload:@"filename.txt" mimeType:MIMEType fileData:data params:params];
}
@end