下载:
/* 加载网络数据
NSString *string = [[[responseObject objectForKey:@"res"] objectForKey:@"data"]objectForKey:@"icon"];string = [@"http://114.80.166.42/pics/" stringByAppendingString:string];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Starting: %@", string);
UIImage *avatarImage = nil;
NSURL *url = [NSURL URLWithString:string];
NSData *responseData = [NSData dataWithContentsOfURL:url];
avatarImage = [UIImage imageWithData:responseData];
NSLog(@"Finishing: %@", string);
if (avatarImage) {
dispatch_async(dispatch_get_main_queue(), ^{
touxiang.image = avatarImage;
});//dispatch_async(dispatch_get_main_queue(),completion);
}
else {
NSLog(@"-- impossible download: %@", string);
}
});
*/
上传:
表单上传的格式~~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mongo Gridfs</title>
</head>
<body>
<form action="gfs.php" method="post" enctype="multipart/form-data">
<input type="file" name="upfile" />
<input type="submit" value="upload" /> <a href="gfs.php">查看图片</a>
</form>
</body>
</html>
我的ios代码~~~
//dd是获取的图片data
NSData *dd = [NSData dataWithContentsOfFile:filePath];
NSString *url =@"http://192.168.1.27/LBSAPP/gfs.php";
//分界线的标识符
NSString *TWITTERFON_FORM_BOUNDARY = @"AaB03x";
//根据url初始化request
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10];
//分界线 --AaB03x
NSString *MPboundary=[[NSString alloc]initWithFormat:@"--%@",TWITTERFON_FORM_BOUNDARY];
//结束符 AaB03x--
NSString *endMPboundary=[[NSString alloc]initWithFormat:@"%@--",MPboundary];
NSMutableString *body=[[NSMutableString alloc]init];
添加分界线,换行
[body appendFormat:@"%@\r\n",MPboundary];
//声明pic字段,文件名为boris.png
[body appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=\"999999.png\"\r\n"];
//声明上传文件的格式
[body appendFormat:@"Content-Type: image/png\r\n\r\n"];
//[body appendFormat:@"Content-Type: text/html\r\n\r\n"];
//声明结束符:--AaB03x--
NSString *end=[[NSString alloc]initWithFormat:@"\r\n%@",endMPboundary];
//声明myRequestData,用来放入http body
NSMutableData *myRequestData=[NSMutableData data];
//将body字符串转化为UTF8格式的二进制
[myRequestData appendData:[body dataUsingEncoding:NSUTF8StringEncoding]];
//将image的data加入
[myRequestData appendData:dd];
//加入结束符--AaB03x--
[myRequestData appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
//设置HTTPHeader中Content-Type的值
NSString *content=[[NSString alloc]initWithFormat:@"multipart/form-data; boundary=%@",TWITTERFON_FORM_BOUNDARY];
//设置HTTPHeader
[request setValue:content forHTTPHeaderField:@"Content-Type"];
//设置Content-Length
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[myRequestData length]] forHTTPHeaderField:@"Content-Length"];
//设置http body
[request setHTTPBody:myRequestData];
//http method
[request setHTTPMethod:@"POST"];
//建立连接,设置代理
NSError *error = nil;
NSData *conn = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
if (error) {
NSLog(@"%@",error);
}
//设置接受response的data
if (conn) {
//NSMutableData * mResponseData = [NSMutableData data] ;
NSString *ok = [[NSString alloc]initWithData:conn encoding:NSUTF8StringEncoding];
NSLog(@"%@",ok);
}
}