iOS中用系統提供的API能實現能實現文件的上傳與下載,分別有兩種方式。NSURLConnection與NSURLSession。
其中NSURLConnection是使用很久的的一種方式,NSURLSession是新出來的一種方式。
一、 POST方式上傳
其他平臺做的好一點的可能封裝好了,不需要自己拼接字符串格式。因此iOS中很少用這種方式上傳。
示例代碼:
#import "XNUploadFile.h"
#define kTimeOut 5.0f
@implementation XNUploadFile
/** 分隔字符串 */
static NSString *boundaryStr = @"--";
/** 本次上傳標示字符串 */
static NSString *randomIDStr;
/** 上傳(php)腳本中,接收文件字段 */
static NSString *uploadID;
- (instancetype)init
{
self = [super init];
if (self) {
/** 本次上傳標示字符串 */
randomIDStr = @"itcastupload";
/** 上傳(php)腳本中,接收文件字段 */
// 可以諮詢公司的網站開發程序員
// 或者用FireBug自己跟蹤調試
uploadID = @"uploadFile";
}
return self;
}
#pragma mark - 成員方法. 用NSURLSession來完成上傳
- (void)uploadFile:(NSString *)path fileName:(NSString *)fileName completion:(void (^)(NSString *string))completion
{
// 1. url 提示:真正負責文件上傳的是php文件,而不是html文件
NSURL *url = [NSURL URLWithString:@"http://localhost/new/post/upload.php"];
// 2. request
NSURLRequest *request = [self requestForUploadURL:url uploadFileName:fileName localFilePath:path];
// 3. session(回話)
// 全局網絡回話,爲了方便程序員使用網絡服務
NSURLSession *session = [NSURLSession sharedSession];
// 4. 數據任務-> 任務都是由回話發起的
/** URLSession的任務,默認都是在其他線程工作的,默認都是異步的 */
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@ %@", result, [NSThread currentThread]);
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) {
completion(@"下載完成");
}
});
}] resume];
// NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//
// id result = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
//
// NSLog(@"%@ %@", result, [NSThread currentThread]);
//
// dispatch_async(dispatch_get_main_queue(), ^{
// if (completion) {
// completion(@"下載完成");
// }
// });
// }];
//
// // 5. 啓動任務
// [task resume];
}
#pragma mark - 私有方法 : 拼字符串
/** 拼接頂部字符串 */
- (NSString *)topStringWithMimeType:(NSString *)mimeType uploadFile:(NSString *)uploadFile
{
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];
[strM appendFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\n", uploadID, uploadFile];
[strM appendFormat:@"Content-Type: %@\n\n", mimeType];
NSLog(@"頂部字符串:%@", strM);
return [strM copy];
}
/** 拼接底部字符串 */
- (NSString *)bottomString
{
NSMutableString *strM = [NSMutableString string];
[strM appendFormat:@"%@%@\n", boundaryStr, randomIDStr];
[strM appendString:@"Content-Disposition: form-data; name=\"submit\"\n\n"];
[strM appendString:@"Submit\n"];
[strM appendFormat:@"%@%@--\n", boundaryStr, randomIDStr];
NSLog(@"底部字符串:%@", strM);
return [strM copy];
}
/** 指定全路徑文件的mimeType */
- (NSString *)mimeTypeWithFilePath:(NSString *)filePath
{
// 1. 判斷文件是否存在
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
return nil;
}
// 2. 使用HTTP HEAD方法獲取上傳文件信息
NSURL *url = [NSURL fileURLWithPath:filePath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 3. 調用同步方法獲取文件的MimeType
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:NULL];
return response.MIMEType;
}
/** 上傳文件網絡請求 */
- (NSURLRequest *)requestForUploadURL:(NSURL *)url uploadFileName:(NSString *)fileName localFilePath:(NSString *)filePath
{
// 0. 獲取上傳文件的mimeType
NSString *mimeType = [self mimeTypeWithFilePath:filePath];
if (!mimeType) return nil;
// 1. 拼接要上傳的數據體
NSMutableData *dataM = [NSMutableData data];
[dataM appendData:[[self topStringWithMimeType:mimeType uploadFile:fileName] dataUsingEncoding:NSUTF8StringEncoding]];
// 拼接上傳文件本身的二進制數據
[dataM appendData:[NSData dataWithContentsOfFile:filePath]];
[dataM appendData:[[self bottomString] dataUsingEncoding:NSUTF8StringEncoding]];
// 2. 設置請求
NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:kTimeOut];
// 1> 設定HTTP請求方式
requestM.HTTPMethod = @"POST";
// 2> 設置數據體
requestM.HTTPBody = dataM;
// 3> 指定Content-Type
NSString *typeStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];
[requestM setValue:typeStr forHTTPHeaderField:@"Content-Type"];
// 4> 指定數據長度
NSString *lengthStr = [NSString stringWithFormat:@"%@", @([dataM length])];
[requestM setValue:lengthStr forHTTPHeaderField:@"Content-Length"];
return [requestM copy];
}
二、 PUT方式上傳
- (void)putFile
{
// 1. url 最後一個是要上傳的文件名
NSURL *url = [NSURL URLWithString:@"http://localhost/uploads/abcd"]; //abcd爲文件名
// 2. request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"PUT";
// request.HTTPMethod = @"DELETE";
// 設置用戶授權
// BASE64編碼:一種對字符串和二進制數據進行編碼的一種“最常用的網絡編碼方式”,此編碼可以將二進制數據轉換成字符串!
// 是很多加密算法的底層算法
// BASE64支持反編碼,是一種雙向的編碼方案
NSString *authStr = @"admin:123";
NSString *authBase64 = [NSString stringWithFormat:@"Basic %@", [self base64Encode:authStr]];
[request setValue:authBase64 forHTTPHeaderField:@"Authorization"];
// 3. URLSession
NSURLSession *session = [NSURLSession sharedSession];
// 4. 由session發起任務
NSURL *localURL = [[NSBundle mainBundle] URLForResource:@"001.png" withExtension:nil];
[[session uploadTaskWithRequest:request fromFile:localURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"sesult---> %@ %@", result, [NSThread currentThread]);
}] resume];
}
- (NSString *)base64Encode:(NSString *)str
{
// 1. 將字符串轉換成二進制數據
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
// 2. 對二進制數據進行base64編碼
NSString *result = [data base64EncodedStringWithOptions:0];
NSLog(@"base464--> %@", result);
return result;
}
PUT方式與DELETE對應,DELETE用於刪除PUT方式上傳的文件。
TIPS:session使用注意