(iPhone/iPad)文件上传与下载

文件下载:

-(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.html

3.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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值