zoukankan      html  css  js  c++  java
  • 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:@"--%@
    ", boundary];
        [startStr appendString:@"Content-Disposition: form-data; name="file"; filename="upload.png"
    "];
        [startStr appendString:@"Content-Type: image/png
    
    "];
        
        //3>尾部字符串
        NSMutableString *endStr = [NSMutableString string];
        [endStr appendFormat:@"--%@
    ", boundary];
        [endStr appendString:@"Content-Disposition: form-data: name="submit"
    
    "];
        [endStr appendString:@"Submit
    "];
        [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



  • 相关阅读:
    一些坑点
    [Luogu P4168] [Violet]蒲公英 (分块)
    冬令营颓废笔记
    WC2019 填坑记
    [Luogu P1829] [国家集训队]Crash的数字表格 / JZPTAB (莫比乌斯反演)
    [Luogu P2522] [HAOI2011]Problem b (莫比乌斯反演)
    [Luogu P3327] [SDOI2015]约数个数和 (莫比乌斯反演)
    [Luogu P3455] [POI2007]ZAP-Queries (莫比乌斯反演 )
    [Luogu P2257] YY的GCD (莫比乌斯函数)
    杭电 1166 敌兵布阵 (线段树)
  • 原文地址:https://www.cnblogs.com/yfceshi/p/7257630.html
Copyright © 2011-2022 走看看