完整代码:

NSURL *url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@%@",[utils getAppUploadMeidaUrl],UploadPIC] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

ASIFormDataRequest  *dictonary = [ASIFormDataRequest requestWithURL:url];

[dictonarysetRequestMethod:@"POST"];

//改用这个方法设置header

NSMutableDictionary *header = [[NSMutableDictionary alloc] init];

[headersetValue:@"multipart/form-data" forKey:@"Content-Type"];

[dictonarysetRequestHeaders:header];

[dictonary  setPostValue:[utils getUnitID]  forKey:@"unitId"];

[dictonary  setPostValue:model.componentsTypeName   forKey:@"objName"];//部件名称

[dictonary  setPostValue:model.componentsCategory   forKey:@"componentsCategory"];//部件大类

[dictonary  setPostValue:model.componentsType  forKey:@"componentsType"];//部件小类

//上传图片

if(self.arrPic.count) {

for(UIImage*eImage in self.arrPic) {

intx =arc4random() %100;

inty =arc4random() %100;

NSData*imageData=UIImageJPEGRepresentation(eImage,100);

NSString*photoName=[NSStringstringWithFormat:@"%zd-%zd.jpg",x,y];

//照片content

[dictonary  addData:imageData   withFileName:photoName  andContentType:@"image/jpeg"  forKey:@"picFile"];

}

}else{

//无照片时,后台规定也必须传 picFile 字段

[dictonary  addData:@"" withFileName:@""  andContentType:@"image/jpeg"  forKey:@"picFile"];

}

[dictonary buildPostBody];

dictonary.shouldAttemptPersistentConnection=NO;

[dictonary setDelegate:self];

[dictonary setDidFailSelector:@selector(responseFailed)];

[dictonary setDidFinishSelector:@selector(responseComplete:)];

[dictonary setTimeOutSeconds:30];

[dictonary startSynchronous];

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐