• ASIHTTPRequest框架使用总结系列之阿堂教程5(上传数据)


    上篇文章中,阿堂和网友们分享了如何用ASIHTTPRequest框架下载数据的实例,本篇阿堂将数据介绍如何用ASIHTTPRequest框架上传数据的应用实例。

          数据上传是通过ASIHTTPRequest类实现的。前面提到,ASIFormDataRequest相当于html的表单,当用户通过submit按钮提交给服务器。因此,ASIFormDataRequest请求对象的作用相当于提交表单数据,默认采用post请求方法。
     
         下面,阿堂提供的这个实例是将项目应应用中的test1.jpg上传到服务端,然后,接将将服务端的test1.jsp又下载到界面中再显示出来。
     
         ASIHTTPRequest框架使用总结系列之阿堂教程5(上传数据)
    ASIHTTPRequest框架使用总结系列之阿堂教程5(上传数据)

     
        本应用使用ASIHTTPRequest框架上传,下载文件的主要调用代码如下
    ViewController.h,  ViewController.m
     

    #import

    #import "ASIHTTPRequest.h"

    #import "ASIFormDataRequest.h"

    #import "NSNumber+Message.h"

    #import "NSString+URLEncoding.h"

    @interface ViewController : UIViewController

    @property (weak, nonatomic) IBOutlet UIImageView *imageView1;

    @property (weak, nonatomic) IBOutlet UIImageView *imageView2;

    - (IBAction)onClick:(id)sender;

    @end

     
    ------------------------------------------------------------------

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    - (void)viewDidLoad

    {

        [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    }

    - (void)didReceiveMemoryWarning

    {

        [super didReceiveMemoryWarning];

        // Dispose of any resources that can be recreated.

    }

    - (IBAction)onClick:(id)sender {

        

        NSString *strURL1 = @"http://www.crazyit.com/service/upload.php";

        

    NSURL *url1 = [NSURL URLWithString:[strURL1 URLEncodedString]];

        

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url1];

        [request setPostValue:@"heyitang@qq.com" forKey:@"email"];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"jpg"];

        

        [request setFile:path forKey:@"file"];

        [request setDelegate:self];

        [request setDidFinishSelector:@selector(requestSuccess:)];

        [request setDidFailSelector:@selector(requestError:)];

        [request startAsynchronous];

        

    }

    - (void)requestSuccess:(ASIHTTPRequest *)request

    {

        NSData *data = [request responseData];

        NSError *eror;

        NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:&eror];

        if (!resDict) {

            

            //查看图片

            [self seeImage];

            

        } else {

            NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];

            

            NSString *errorStr = [resultCodeObj errorMessage];

            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误信息"

                                                                message:errorStr

                                                               delegate:nil

                                                      cancelButtonTitle:@"OK"

                                                      otherButtonTitles: nil];

            [alertView show];

        }

    }

    - (void)requestError:(ASIHTTPRequest *)request

    {

        NSError *error = [request error];

        NSLog(@"%@",[error localizedDescription]);

    }

    -(void)seeImage

    {

        NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.crazyit.com/service/download.php?email=%@&FileName=test1.jpg",@"heyitang@qq.com"];

        

        NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];

        

        __weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

        [request setCompletionBlock:^{

            

            NSData *data  = [request responseData];

            

            UIImage *img = [UIImage imageWithData:data];

            _imageView1.image = img;

            

        }];

        [request setFailedBlock:^{

            NSError *error = [request error];

            NSLog(@"%@", [error localizedDescription]);

            

        }];

        [request startAsynchronous];

    }

    @end

  • 相关阅读:
    click事件——背景高亮
    color——RGB转16进制
    导航栏高亮设置
    layui table 渲染完成后,怎样拿到表个里的所有数据
    解决 AttributeError: 'dict' object has no attribute 'has_key' 错误的方法
    2019年总结:醒悟还为时不晚
    Worker Services读取配置后,发布Windows出现的问题及解决
    C# 读取配置(详细操作,让我们一起共同成长)
    程序不包含适合于入口点的静态“Main”方法
    .NET Core3.0-Worker Services
  • 原文地址:https://www.cnblogs.com/lisa090818/p/4250037.html
Copyright © 2020-2023  润新知