1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > iOS网络 POST模拟表单上传单个与多个文件(直接调用分类里的方法即可)

iOS网络 POST模拟表单上传单个与多个文件(直接调用分类里的方法即可)

时间:2023-11-30 07:50:03

相关推荐

iOS网络 POST模拟表单上传单个与多个文件(直接调用分类里的方法即可)

独角兽企业重金招聘Python工程师标准>>>

//// ViewController.m// 01-POST上传文件//// Created by 刘凡 on 15/2/8.// Copyright (c) itcast. All rights reserved.//#import "ViewController.h"#import "NSMutableURLRequest+Multipart.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {[self multiPostUpload];}// MARK: - 多个文件上传- (void)multiPostUpload {NSURL *url = [NSURL URLWithString:@"http://localhost/post/upload-m.php"];NSURL *fileUrl1 = [[NSBundle mainBundle] URLForResource:@"demo.jpg" withExtension:nil];NSData *data1 = [NSData dataWithContentsOfURL:fileUrl1];NSURL *fileURL2 = [[NSBundle mainBundle] URLForResource:@"001.png" withExtension:nil];NSData *data2 = [NSData dataWithContentsOfURL:fileURL2];NSArray *dataArray = @[data1, data2];NSArray *fileNames = @[@"demo.jpg", @"001.png"];// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileDataArray:dataArray fileNames:fileNames];// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileURLs:@[fileUrl1, fileURL2] fileNames:fileNames];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileURLs:@[fileUrl1, fileURL2]];// 3. connection[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]);}];}// MARK: - 单个文件上传- (void)postUpload {// 1. urlNSURL *url = [NSURL URLWithString:@"http://localhost/post/upload.php"];// 2. requestNSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"demo.jpg" withExtension:nil];NSData *data = [NSData dataWithContentsOfURL:fileUrl];NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url keyName:@"userfile" fileData:data fileName:@"bcd.jpg"];// 3. connection[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL]);}];}@end

/s/1ntqTaWT

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。