用ASI做图片上传

有些服务器返回的数据,用AFN解析不了,原生的上传图片的方法,代码太长,所以用了ASI上传,代码如下:

               UIImage *image =  [self.photoArrayM[i] originImage];
                
                
                // 1.URL
                NSURL *url = [NSURL URLWithString:[baseUrl stringByAppendingString:@"/upload/uploadFile.action"]];
                
                // 2.创建一个请求对象
                ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
//                [request setPostValue:@"myFile" forKey:@"file"];
//                [request setPostValue:@"提交" forKey:@"submit"];
                [request setData:UIImageJPEGRepresentation(image,0.1)
                 
                    withFileName:[NSString stringWithFormat:@"%d.jpg",i]
                 
                  andContentType:@"image/jpeg/png"
                 
                          forKey:[NSString stringWithFormat:@"file%d",i]];
                
                // 3.监听请求结果
                __weak typeof(request) weakRequest = request;
                [request setCompletionBlock:^{
                    NSLog(@"wawa%@", [weakRequest responseString]);
                }];
                [request setFailedBlock:^{
                    
                    NSLog(@"上传失败");
                    
                }];
                // 4.开始请求
                [request startAsynchronous];

你可能感兴趣的:(用ASI做图片上传)