iOS uploads images via http post

  • 2020-05-24 06:17:04
  • OfStack

The example of this article is to share the code of iOS uploading pictures through http post for your reference. The specific content is as follows


//ASIFormDataRequest way  POST To upload pictures 
-(NSDictionary *)addPicWithDictionary:(NSDictionary *)sugestDic{
NSDictionary *tempDic=nil;
NSString *url=[NSString stringWithFormat:@"http://182.50.0.62:8095/xianServer/upload/uploadImage?clientType=mobile"];
form = [[[ASIFormDataRequest alloc]
initWithURL:[NSURL URLWithString:url]] autorelease];
[form setTimeOutSeconds:60.0];
form.delegate = self;
// Add a photo 
// The identifier of the boundary 
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
// Dividing line  --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
// terminator  AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
// Add photo photo 
imageView.image=[UIImage imageNamed:@"btn_done_down@2x.png"];
NSData* data = UIImagePNGRepresentation(imageView.image);
NSLog(@"%@",data);
//http body The string 
NSMutableString *body=[[NSMutableString alloc]init];
// All of the set of parameters key A collection of 
NSArray *keys= [sugestDic allKeys];
// traverse keys
for(int i=0;i<[keys count];i++)
{
// Get the current key
NSString *key=[keys objectAtIndex:i];
// if key not pic , value Is the character type, for example name : Boris
if(![key isEqualToString:@"files"])
{
// Add a line break 
[body appendFormat:@"%@\r\n",MPboundary];
// Add field name, replace 2 line 
[body appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
// Add the value of the field 
[body appendFormat:@"%@\r\n",[sugestDic objectForKey:key]];
}
}
if (imageView.image) {
//// Add a line break 
[body appendFormat:@"%@\r\n",MPboundary];
// The statement pic Field, file name boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"files\"; filename=\"boris.png\"\r\n"];
// Declare the format of the uploaded file 
[body appendFormat:@"Content-Type: image/png\r\n\r\n"];
}
// End of declaration: --AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
// The statement myRequestData Used to put http body
NSMutableData *myRequestData=[NSMutableData data];
// will body String to UTF8 The format of 2 Into the system 
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
// will image the data join 
[myRequestData appendData:data];
// Add end character --AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
// Set up the HTTPHeader In the Content-Type The value of the 
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
[form addRequestHeader:@"Content-Type" value:content];
[form addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", [myRequestData length]]];
[form setRequestMethod:@"POST"];
[form startAsynchronous];
[form setDidFailSelector:@selector(requestBeFailed:)];
[form setDidFinishSelector:@selector(requestBeFinished:)];
//  Parse the results obtained 
return tempDic;
}

The above is the entire content of this article, I hope to help you with your study.


Related articles: