如何结合asp.net webservice实现文件上传下载。
1、同步下载文件:
[cpp]
<span style="font-family:宋体;">NSString *urlAsString = @"https://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSError *error = nil;
NSData *data = https://www.cnblogs.com/zhwl/archive/2012/07/13/[NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&error];
/* 下载的数据 */
if (data != nil){
NSLog(@"下载成功");
if ([data writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {
NSLog(@"保存成功.");
}
else
{
NSLog(@"保存失败.");
}
} else {
NSLog(@"%@", error);
} </span>
2.异步下载
[cpp]
<span style="font-family:宋体;">DownLoadingViewController.h
// DownLoadingViewController.h
// DownLoading
//
// Created by skylin zhu on 11-7-30.
// Copyright 2011年 mysoft. All rights reserved.
//
#import
@interface DownLoadingViewController : UIViewController {
NSURLConnection *connection;
NSMutableData *connectionData;
}
@property (nonatomic,retain) NSURLConnection *connection;
@property (nonatomic,retain) NSMutableData *connectionData;
@end
DownLoadingViewController.m
// DownLoadingViewController.m
// DownLoading
//
// Created by skylin zhu on 11-7-30.
// Copyright 2011年 mysoft. All rights reserved.
//
#import "DownLoadingViewController.h"
@implementation DownLoadingViewController
@synthesize connection,connectionData;
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//文件地址
NSString *urlAsString = @"https://files.cnblogs.com/zhuqil/UIWebViewDemo.zip";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSMutableData *data = https://www.cnblogs.com/zhwl/archive/2012/07/13/[[NSMutableData alloc] init];
self.connectionData = https://www.cnblogs.com/zhwl/archive/2012/07/13/data;
[data release];
NSURLConnection *newConnection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self
startImmediately:YES];
self.connection = newConnection;
[newConnection release];
if (self.connection != nil){
NSLog(@"Successfully created the connection");
} else {
NSLog(@"Could not create the connection");
}
}
- (void) connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error{
NSLog(@"An error happened");
NSLog(@"%@", error);
}
- (void) connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data{
NSLog(@"Received data");
[self.connectionData appendData:data];
}
- (void) connectionDidFinishLoading
:(NSURLConnection *)connection{
/* 下载的数据 */
NSLog(@"下载成功");
if ([self.connectionData writeToFile:@"UIWebViewDemo.zip" atomically:YES]) {
NSLog(@"保存成功.");
}
else
{
NSLog(@"保存失败.");
}
/* do something with the data here */
}
- (void) connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response{
[self.connectionData setLength:0];
}
- (void) viewDidUnload{
[super viewDidUnload];
[self.connection cancel];
self.connection = nil;
self.connectionData = https://www.cnblogs.com/zhwl/archive/2012/07/13/nil;
}
@end </span>
上一篇: linux查询天气(方法实例)
下一篇: WEB表格导出为EXCEL文档的方法
推荐阅读
-
如何结合asp.net webservice实现文件上传下载。
-
php如何实现文件上传下载
-
Django 如何实现文件上传下载
-
javascript - ajax结合html5中的file实现文件上传,后台用PHP接收,该如何用PHP接收传过来的formData?
-
如何使用asp.net实现文件和文件夹的复制
-
如何结合asp.net webservice实现文件上传下载。
-
有关asp.net如何实现多个文件同时下载问题相关解答
-
如何使用asp.net实现文件和文件夹的复制
-
通过Vue2.0结合webuploader如何实现文件分片上传功能(详细教程)
-
有关asp.net如何实现多个文件同时下载问题相关解答