文件下载:
-(void)downLoadImages: (NSString *)url saveAsImagesName:(NSString *)name{
ASIHTTPRequest *request;
request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]]autorelease];
[request setDownloadDestinationPath:[[NSHomeDirectory()stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:name]];
[request setNumberOfTimesToRetryOnTimeout:2 ];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]];
if (img) {
[imgPerson setImage:img];
}
}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"连接失败!请检查网络连接或服务是否正常!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
其中:第一个参数url是文件下载地址,第二个参数name是文件保存到本地的文件名。
文件上传:
//图片上传
- (void)uploadImage {
NSData *imageData = UIImagePNGRepresentation(imgPerson.image);
// setting up the URL to post to
NSString *urlString = @"http://www.xxxx.com/lsmobile/UploadServlet";
// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
/*
add some header info now
we always need a boundary when we post a file
also we need to set the content type
You might want to generate a random boundary.. this is just the same
as my output from wireshark on a valid html post
*/
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
/*
now lets create the body of the post
*/
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",strHeadPath] dataUsingEncoding:NSUTF8StringEncoding]];
[strHeadPath release];
strHeadPath=nil;
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSError *errorMsg=nil;
// now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&errorMsg];
if (!errorMsg) {
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
NSLog(returnString);
if ([returnString isEqualToString:@"Fail"]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"sorry,图片上传失败,请尝试重新提交" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}else{
strImgPath=[[NSString alloc] initWithFormat:@"%@",returnString];//在更改操作中release
uploadFlg=true;
}
[returnString release];
returnString=nil;
}else{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"连接失败!请检查网络连接或服务是否正常!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
本段代码中上传的是图片,如果想上传其他文件,可更改最初的NSData数据源。
还有很多方法
更多上传下载方法可参考:
1.iOS开发之结合asp.net webservice实现文件上传下载http://www.cnblogs.com/zhuqil/archive/2011/07/30/2122019.html
2.http://www.flyblog.info/catprogramming/392.html3.http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/
4.http://zcentric.com/2008/08/29/post-a-uiimage-to-the-web/