您的位置:首页 > 理论基础 > 计算机网络

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

2016-03-16 15:28 756 查看
   本人工作使用,测试通过无误

   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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息