IOS文件上传
2016-02-26 17:03
435 查看
IOS文件上传
[objc] view
plaincopy
//
// MJViewController.m
// 02.Post上传
//
// Created by apple on 14-4-29.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJViewController.h"
#import "UploadFile.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UploadFile *upload = [[UploadFile alloc] init];
NSString *urlString = @"http://localhost/upload.php";
NSString *path = [[NSBundle mainBundle] pathForResource:@"头像1.png" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
[upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data];
}
@end
[objc] view
plaincopy
//
// UploadFile.m
// 02.Post上传
//
// Created by apple on 14-4-29.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "UploadFile.h"
@implementation UploadFile
// 拼接字符串
static NSString *boundaryStr = @"--"; // 分隔字符串
static NSString *randomIDStr; // 本次上传标示字符串
static NSString *uploadID; // 上传(php)脚本中,接收文件字段
- (instancetype)init
{
self = [super init];
if (self) {
randomIDStr = @"itcast";
uploadID = @"uploadFile";
}
return self;
}
#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];
}
#pragma mark - 上传文件
- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data
{
// 1> 数据体
NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"];
NSString *bottomStr = [self bottomString];
NSMutableData *dataM = [NSMutableData data];
[dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];
[dataM appendData:data];
[dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];
// 1. Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
// dataM出了作用域就会被释放,因此不用copy
request.HTTPBody = dataM;
// 2> 设置Request的头属性
request.HTTPMethod = @"POST";
// 3> 设置Content-Length
NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];
[request setValue:strLength forHTTPHeaderField:@"Content-Length"];
// 4> 设置Content-Type
NSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];
[request setValue:strContentType forHTTPHeaderField:@"Content-Type"];
// 3> 连接服务器发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", result);
}];
}
@end
[objc] view
plaincopy
//
// MJViewController.m
// 02.Post上传
//
// Created by apple on 14-4-29.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "MJViewController.h"
#import "UploadFile.h"
@interface MJViewController ()
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UploadFile *upload = [[UploadFile alloc] init];
NSString *urlString = @"http://localhost/upload.php";
NSString *path = [[NSBundle mainBundle] pathForResource:@"头像1.png" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:path];
[upload uploadFileWithURL:[NSURL URLWithString:urlString] data:data];
}
@end
[objc] view
plaincopy
//
// UploadFile.m
// 02.Post上传
//
// Created by apple on 14-4-29.
// Copyright (c) 2014年 itcast. All rights reserved.
//
#import "UploadFile.h"
@implementation UploadFile
// 拼接字符串
static NSString *boundaryStr = @"--"; // 分隔字符串
static NSString *randomIDStr; // 本次上传标示字符串
static NSString *uploadID; // 上传(php)脚本中,接收文件字段
- (instancetype)init
{
self = [super init];
if (self) {
randomIDStr = @"itcast";
uploadID = @"uploadFile";
}
return self;
}
#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];
}
#pragma mark - 上传文件
- (void)uploadFileWithURL:(NSURL *)url data:(NSData *)data
{
// 1> 数据体
NSString *topStr = [self topStringWithMimeType:@"image/png" uploadFile:@"头像1.png"];
NSString *bottomStr = [self bottomString];
NSMutableData *dataM = [NSMutableData data];
[dataM appendData:[topStr dataUsingEncoding:NSUTF8StringEncoding]];
[dataM appendData:data];
[dataM appendData:[bottomStr dataUsingEncoding:NSUTF8StringEncoding]];
// 1. Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
// dataM出了作用域就会被释放,因此不用copy
request.HTTPBody = dataM;
// 2> 设置Request的头属性
request.HTTPMethod = @"POST";
// 3> 设置Content-Length
NSString *strLength = [NSString stringWithFormat:@"%ld", (long)dataM.length];
[request setValue:strLength forHTTPHeaderField:@"Content-Length"];
// 4> 设置Content-Type
NSString *strContentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", randomIDStr];
[request setValue:strContentType forHTTPHeaderField:@"Content-Type"];
// 3> 连接服务器发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", result);
}];
}
@end
相关文章推荐
- iOS 代码设置自动布局
- IOS常用正则表达式
- iOS 开发切割圆角图片
- iOS开发全面解析多线程
- ios开发根据Label的字体和宽度算出它的高度
- iOS企业发布问题记录三(Log Guru 使用方法)
- iOS 常见的加密形式: AES加密, DES加密, MD5加密,SHA1加密, BASE64加密
- ios 资源很全面转载以后方便看
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 判断字符串是否中文 & 判断是否正确身份证号
- iOS开发笔记--敏捷开发之Scrum扫盲篇
- iOS Block 的理解与学习
- iOS 蓝牙打印小票机
- iOS多线程编程总结
- iOS实战-自定义的横向滚动控件CustomScrollView
- Win8.1+Ubuntu双系统(BIOS+MBR)
- 深入浅出iOS函数式编程与响应式编程概念
- iOS推送之远程推送
- 深入理解IOS的RunLoop
- IOS学习 info.plist属性列表讲解(IOS)