iOS通过http post上传图片的代码

代码过程,把做工程过程经常用到的一些代码片段备份一下,下面的代码是关于iOS通过http post上传图片的代码,希望对大伙有些用处。

 
     
     
form = [[[ASIFormDataRequest alloc]
initWithURL:[NSURL URLWithString:url]] autorelease];
[form setTimeOutSeconds:60.0];
form.delegate = self;

imageView.image=[UIImage imageNamed:@"[email protected]"];
NSLog(@"%@",data);

for(int i=0;i<[keys count];i++)  
{  
        if(![key isEqualToString:@"files"])
        {  
                [body appendFormat:@"%@rn",MPboundary];  
                [body appendFormat:@"Content-Disposition: form-data; name="%@"rnrn",key];  
                [body appendFormat:@"%@rn",[sugestDic objectForKey:key]];              
        }  
}  
if (imageView.image) {
        [body appendFormat:@"%@rn",MPboundary];
        [body appendFormat:@"Content-Disposition: form-data; name="files"; filename="boris.png"rn"];  
        [body appendFormat:@"Content-Type: image/pngrnrn"];
}

[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]]; 
[myRequestData appendData:data]; 
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];

[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:)];
return tempDic; 

}

猜你喜欢

转载自blog.51cto.com/14406597/2410370
今日推荐