ios网络学习------10 原生API文件上传

Posted yfceshi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios网络学习------10 原生API文件上传相关的知识,希望对你有一定的参考价值。

使用原生态的api上传文件的实现:

#import "MainViewController.h"

@interface MainViewController ()
@property (weak, nonatomic) UIImageView *imageView;
@end

@implementation MainViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    //定义Imageview并设置图像
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    [imageView setFrame:CGRectMake(60, 20, 200, 200)];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(60, 240, 200, 40)];
    
    [button setTitle:@"upload" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(uploadImage) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

#pragma mark 上传文件(图像)
- (void)uploadImage
{
    NSLog(@"upload");
    //思路: 须要使用http的post方法上传文件
    //调用的url是http://localhost/~apple/itcast/upload.php
    //数据体的參数名:file
    
    //1建立URL
    NSURL *url = [NSURL URLWithString:@"http://localhost/~apple/itcast/upload.php"];
    //2建立NSMutableRequest
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //1)设置request的属性。设置方法
    [request setHTTPMethod:@"POST"];
    
    
    
    
    //2)设置数据体
    //1> 设置boundary的字符串,能够复用
    
    NSString *boundary = @"uploadBoundary";
    //2>头部字符串
    
    NSMutableString *startStr = [NSMutableString string];
    [startStr appendFormat:@"--%@\n", boundary];
    [startStr appendString:@"Content-Disposition: form-data; name=\"file\"; filename=\"upload.png\"\n"];
    [startStr appendString:@"Content-Type: image/png\n\n"];
    
    //3>尾部字符串
    NSMutableString *endStr = [NSMutableString string];
    [endStr appendFormat:@"--%@\n", boundary];
    [endStr appendString:@"Content-Disposition: form-data: name=\"submit\"\n\n"];
    [endStr appendString:@"Submit\n"];
    [endStr appendFormat:@"--%@--", boundary];
    
    //4>拼接数据体
    NSMutableData *bodyData = [NSMutableData data];
    [bodyData appendData:[startStr dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *imageData = UIImagePNGRepresentation(self.imageView.image);
    [bodyData appendData:imageData];
    [bodyData appendData:[endStr dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:bodyData];
    
    //5>指定Content-Type,在上传文件时,须要指定content-type和content-length
    NSString *contentStr = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentStr forKey:@"Content-Type"];
    
    //6>指定Content-Length
    NSInteger length = [bodyData length];
    [request setValue:[NSString stringWithFormat:@"%d", length] forKey:@"Content-Length"];
    
    
    
    
    //3使用NSURLConnection的同步方法上传文件。由于须要用户确认文件是否上传成功。

//在使用http上传文件时,一般是有限制大小的。一般不会超过2M. NSURLResponse *response = nil; NSError *error = nil; NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *resultStr = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding]; NSLog(@"%@", resultStr); } @end





以上是关于ios网络学习------10 原生API文件上传的主要内容,如果未能解决你的问题,请参考以下文章

ios开发网络学习十二:NSURLSession实现文件上传

Flutter笔记-调用原生IOS高德地图sdk

原生API实现拖拽上传文件实践

安卓网络编程学习——java原生网络编程

我可以在 Xcode 中使用 Spotify Web API 来开发原生 iOS 应用程序吗?

trigger.io 如何处理文件上传请求中的带宽限制、超时和重试?