IOS开发基础之单文件上传基础最原始的方式
发布时间:2023-04-06 11:44:39 所属栏目:教程 来源:
导读:IOS开发基础之单文件上传基础最原始的方式info.plist加入一行代码<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>////ViewController.m//05-上传单个文件////Createdby鲁军
|
IOS开发基础之单文件上传基础最原始的方式info.plist加入一行代码<key>NSAppTransportSecurity</key><dict><key>NSAllowsArbitraryLoads</key><true/></dict>////ViewController.m//05-上传单个文件////Createdby鲁军 IOS开发基础之单文件上传基础最原始的方式 info.plist 加入一行代码 <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> 在这里插入图片描述 // // ViewController.m // 05-上传单个文件 // // Created by 鲁军 on 2021/2/13. // #import "ViewController.h" #define kBOUNDARY @"abc" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //@"" // [self uploadFile]; Nsstring *path=[[NSBundle mainBundle] pathForResource:@"IOSThreadNetwork.zip" ofType:nil]; [self uploadFile:@"" fieldName:@"file" filePath:path]; } -(void)uploadFile:(Nsstring *)urlString fieldName:(Nsstring *)fieldName filePath:(Nsstring *)filePath{ NSURL *url =[NSURL URLWithString:urlString]; NSMutableuRLRequest *request=[NSMutableuRLRequest requestWithURL:url]; request.HTTPMethod=@"post"; [request setValue:[Nsstring stringWithFormat:@"multipart/form-data;boundary=%@",kBOUNDARY] forHTTPHeaderField:@"Content-Type"]; //request.HTTPBody=[self makeBody]; request.HTTPBody=[self makeBody:fieldName filePath:filePath]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if(connectionError){ NSLog(@"连接错误 %@",connectionError); return; } NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if(httpResponse.statusCode==200||httpResponse.statusCode==304){ //解析数据 id json =[NSJSONSerialization JSONObjectWithData:data options:nil error:NULL]; NSLog(@"%@",json); }else{ NSLog(@"服务器内部错误"); } }]; } -(void)uploadFile1{ NSURL *url =[NSURL URLWithString:@""]; NSMutableuRLRequest *request=[NSMutableuRLRequest requestWithURL:url]; request.HTTPMethod=@"post"; [request setValue:[Nsstring stringWithFormat:@"multipart/form-data;boundary=%@",kBOUNDARY] forHTTPHeaderField:@"Content-Type"]; request.HTTPBody=[self makeBody]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { if(connectionError){ NSLog(@"连接错误 %@",connectionError); return; } NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if(httpResponse.statusCode==200||httpResponse.statusCode==304){ //解析数据 id json =[NSJSONSerialization JSONObjectWithData:data options:nil error:NULL]; NSLog(@"%@",json); }else{ NSLog(@"服务器内部错误"); } }]; } -(NSData *)makeBody:(Nsstring *)fieldName filePath:(Nsstring *)filePath{ NSMutableData *mData=[NSMutableData data]; NSMutableString *mString=[NSMutableString string]; [mString appendFormat:@"--%@\r\n",kBOUNDARY]; [mString appendFormat:@"Content-disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",fieldName,[filePath lastPathComponent]]; [mString appendString:@"Content-Type: application/octet-stream\r\n"]; [mString appendString:@"\r\n"]; [mData appendData:[mString dataUsingEncoding:NSUTF8StringEncoding]]; //第二部分 // 加载文件 // Nsstring *path =[[NSBundle mainBundle] pathForResource:@"07.jpg" ofType:nil]; NSData *data =[NSData dataWithContentsOfFile:filePath]; [mData appendData:data]; Nsstring *end=[Nsstring stringWithFormat:@"\r\n--%@--",kBOUNDARY]; [mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]]; return mData.copy; } -(NSData *)makeBody{ NSMutableData *mData=[NSMutableData data]; NSMutableString *mString=[NSMutableString string]; [mString appendFormat:@"--%@\r\n",kBOUNDARY]; [mString appendString:@"Content-disposition: form-data; name=\"file\"; filename=\"07.jpg\"\r\n"]; [mString appendString:@"Content-Type: image/jpeg\r\n"]; [mString appendString:@"\r\n"]; [mData appendData:[mString dataUsingEncoding:NSUTF8StringEncoding]]; //第二部分 // 加载文件 Nsstring *path =[[NSBundle mainBundle] pathForResource:@"07.jpg" ofType:nil]; NSData *data =[NSData dataWithContentsOfFile:path]; [mData appendData:data]; Nsstring *end=[Nsstring stringWithFormat:@"\r\n--%@--",kBOUNDARY]; [mData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]]; return mData.copy; } @end (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
