iOS上传文件到服务器(ASIHttpRequest)

来源:互联网 发布:flac mp3 mac 编辑:程序博客网 时间:2024/04/29 14:59

   本人工作使用,测试通过无误

   ASIHttpRequest(下载地址):http://www.oschina.net/p/asihttprequest

  导入头文件

 #import "ASIFormDataRequest.h"


//上传数据

//    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://sview.sv3d.cn/quickpass/upload"]];

    //http://10.0.88.214:8080/SViewWeb/quickpass/upload测试用

    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://10.0.88.214:8080/SViewWeb/quickpass/upload"]];

       [request setFile:self.fileRoute  forKey:@"modelfile"];//上传文件

       [request setFile:self.imagePath  forKey:@"upimage"];

       [request setUploadProgressDelegate:self];//设置上传进度代理

       request.showAccurateProgress = YES;

       request.delegate=self;

       [request setPostValue:self.fileTextView.text forKey:@"description"];

       [request setPostValue:self.nameField.text forKey:@"name"];

       [request setDidFailSelector:@selector(requestDidFailed:)];//上传失败

       [request setDidFinishSelector:@selector(requestDidSuccess:)];//上传成功

       [request startAsynchronous];

}

//执行成功

- (void)requestDidSuccess:(ASIFormDataRequest *)request

{

    //获取头文件

    NSDictionary *headers = [request responseHeaders];

    NSString *method = [request.userInfo objectForKey:@"Method"];

    //获取http协议执行代码

    NSLog(@"Code:%d",[request responseStatusCode]);

    if ([self respondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])

    {

        //执行委托操作 (架构设计   自选)

        [self OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];

    }

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

//执行失败

- (void)requestDidFailed:(ASIFormDataRequest *)request{

        //获取的用户自定义内容

        NSString *method = [request.userInfo objectForKey:@"Method"];

        //获取错误数据

        NSError *error = [request error];

        if ([self respondsToSelector:@selector(OARequestFailed:withError:)])

        {

            //执行委托将错误数据传其他方式(架构设计   自选)

            [self OARequestFailed:method withError:error];

        }

          [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

    }


//上传执行成功函数


- (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers

{

    

   self.responseStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

 //   NSRange range3 = [self.responseStr rangeOfString:@","];

 //   self.uuid = [self.responseStr substringFromIndex:range3.location+1];

 //   NSLog(@"%@",self.uuid);

    //服务返回post后的数据,返回OK+uuid,根据需要得到相应的字符串

    NSLog(@"response:\n%@",self.responseStr);

 //   [self createProgressView];

 //   [self reSaveHistoryNotes]; 上传历史记录,数据持久化存储

}


  //上传执行失败函数


- (void)OARequestFailed:(NSString *)method withError:(NSError *)error

{

    NSLog(@"Error:%@",error);

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错了" message:@"网络连接失败,请稍后重试." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];

    [alert show];

}



0 0