这个是转载的马上验证
“[formData appendPartWithFileURL:url name:@"userfile" fileName:@"1.zip" mimeType:@"application/octet-stream" error:NULL];“
上传过程中,需要核对对应的参数 ,第一个是文件地址NSURL类型,第二个是自定义的文件名称,第三个是等待上传的文件名称,第四个是文件上传的方式(哪种流类型),可以通过搜索 mimeType 来找到对应的上传类型
#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController () <</span>NSURLSessionTaskDelegate>
@property (nonatomic, strong) NSURLSession *session;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self uploadFile];
[self uploadFile2];
}
- (void)uploadFile
{
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
[manger POST:@"http://localhost/post/upload.php" parameters:nil constructingBodyWithBlock:^(id<</span>AFMultipartFormData> formData) {
NSURL *url = [[NSBundle mainBundle]URLForResource:@"music.mp3.zip" withExtension:nil];
[formData appendPartWithFileURL:url name:@"userfile" fileName:@"1.zip" mimeType:@"application/octet-stream"error:NULL];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败");
}];
}
- (void)uploadFile2
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURLURLWithString:@"http://localhost/post/upload.php"]cachePolicy:1 timeoutInterval:7];
request.HTTPMethod = @"post";
// boundary可随意命名
NSString *boundary = @"chen";
// 拼接请求头
[request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]forHTTPHeaderField:@"Content-Type"];
// 创建可变data 后面一样拼接
NSMutableData *myData = [NSMutableData data];
NSString *str = [NSString stringWithFormat:@"--%@\n",boundary];
[myData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
// html页面上传表单里的
NSString *name = @"userfile";
// 上传后文件的名字
NSString *filename = @"1.zip";
str = [NSString stringWithFormat:@"Content-Disposition: form-data; name="%@"; filename="%@"\n",name,filename];
[myData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
str = @"Content-Type: application/octet-stream\n\n";
[myData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
// bundle中的文件转换成二进制数据
NSURL *uploadFile = [[NSBundle mainBundle]URLForResource:@"music.mp3.zip" withExtension:nil];
[myData appendData:[NSData dataWithContentsOfURL:uploadFile]];
str = [NSString stringWithFormat:@"\n\n--%@--",boundary];
[myData appendData:[str dataUsingEncoding:NSUTF8StringEncoding]];
request.HTTPBody = myData;
[[self.session uploadTaskWithRequest:request fromData:myData]resume];
}
#pragma mark - 检测上传进度
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didSendBodyData:(int64_t)bytesSent
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
float progress = (float)totalBytesSent / totalBytesExpectedToSend;
NSLog(@"%f %@", progress, [NSThread currentThread]);
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"完成");
}
// 懒加载
- (NSURLSession *)session
{
if(_session == nil)
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
}
return _session;
}
@end