最近尝试着使用ASIHttpRequest对服务器的文件进行下载,但经过多次尝试,发现仅仅下载png的图片时,下载的文件大小与原文件一致,其他如rar、doc、txt等文件格式,下载的文件大小均要比原文件小,导致下载的文件不能正常使用。关键代码如下:- (IBAction)downloadCert:(id)sender {
if ( fm == nil ) {
fm =[NSFileManager defaultManager];
}
// NSString *userDocPath=[NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle]resourcePath],@"/"]; NSString *userDocPath=[NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) objectAtIndex : 0];
// file1
NSString *file1= @"file1.doc" ;
NSURL *url1 = [NSURL URLWithString : @"http://10.7.13.111:8080/WebContent/file/file1.doc"];
// 先创建文件 file1 ,再用 NSFileHandle 打开它
NSString *path1=[userDocPath stringByAppendingPathComponent :file1];
bool b=[fm createFileAtPath :path1 contents : nil attributes : nil];
NSFileHandle *fh1;
__block uint fSize1= 0 ; // 以 B 为单位,记录已下载的文件大小 , 需要声明为块可写
if (b){
fh1=[NSFileHandle fileHandleForWritingAtPath :path1];
}
// file2
NSString *file2= @"file2.png" ;
NSURL *url2 = [NSURL URLWithString : @"http://10.7.13.111:8080/WebContent/file/file2.png"];
// 先创建文件 file2 ,再用 NSFileHandle 打开它
NSString *path2=[userDocPath stringByAppendingPathComponent :file2];
b=[fm createFileAtPath :path2 contents : nil attributes : nil];
NSFileHandle *fh2;
__block uint fSize2= 0 ; // 以 B 为单位,记录已下载的文件大小 , 需要声明为块可写
if (b){
fh2=[NSFileHandle fileHandleForWritingAtPath :path2];
}
//////////////////////////// 任务队列 /////////////////////////////
if (! networkQueue ) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[networkQueue reset]; // 队列清零
[networkQueue setDownloadProgressDelegate : progress_total]; // 设置 queue 进度条
[networkQueue setShowAccurateProgress : YES]; // 进度精确显示
[networkQueue setDelegate : self]; // 设置队列的代理对象
ASIHTTPRequest *request;
///////////////// request for file1 //////////////////////
request = [ASIHTTPRequest requestWithURL :url1]; // 设置file1的 url
[request setDownloadProgressDelegate : progress_file1]; // file1的下载进度条
// 设置 userInfo ,可用于识别不同的 request 对象
[request setUserInfo :[NSDictionary dictionaryWithObject :file1 forKey : @"file1"]];
// 使用 complete 块,在下载完时做一些事情
[request setCompletionBlock :^( void ){
NSLog ( @"%@ complete !" ,file1);
assert (fh1);
// 关闭 file1
[fh1 closeFile];
}];
// 使用 failed 块,在下载失败时做一些事情
[request setFailedBlock :^( void ){
failed = YES;
NSLog ( @"%@ download failed !" ,file1);}
];
// 使用 received 块,在接受到数据时做一些事情
[request setDataReceivedBlock :^( NSData * data){
fSize1+=data. length ;
[status_file1 setText :[NSString stringWithFormat : @"%i B" ,fSize1]];
[status_total setText :[NSString stringWithFormat : @"%.0f %%" , progress_total . progress * 100]];
if (fh1!= nil ) {
[fh1 seekToEndOfFile];
[fh1 writeData :data];
}
NSLog ( @"%@:%u" ,file1,data.length );
}];
[networkQueue addOperation :request];
///////////// request for file2 //////////////////
request = [[ASIHTTPRequest alloc] initWithURL :url2]; // 设置file2的 url
[request setDownloadProgressDelegate : progress_file2]; // file2的下载进度条
[request setUserInfo :[NSDictionary dictionaryWithObject :file2 forKey : @"file2"]];
// 使用 complete 块,在下载完时做一些事情
[request setCompletionBlock :^( void ){
NSLog ( @"%@ complete !" ,file2);
assert (fh2);
// 关闭 file2
[fh2 closeFile];
}];
// 使用 failed 块,在下载失败时做一些事情
[request setFailedBlock :^( void ){
failed = YES;
NSLog ( @"%@ download failed !" ,file2);
}];
// 使用 received 块,在接受到数据时做一些事情
[request setDataReceivedBlock :^( NSData * data){
fSize2+=data. length ;
[status_file2 setText :[NSString stringWithFormat : @"%i B" ,fSize2]];
[status_total setText :[NSString stringWithFormat : @"%.0f %%" , progress_total . progress * 100]];
if (fh2!= nil ) {
[fh2 seekToEndOfFile];
[fh2 writeData :data];
}
}];
[networkQueue addOperation :request];
[networkQueue go]; // 队列任务开始
}
if ( fm == nil ) {
fm =[NSFileManager defaultManager];
}
// NSString *userDocPath=[NSString stringWithFormat:@"%@%@",[[NSBundle mainBundle]resourcePath],@"/"]; NSString *userDocPath=[NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory , NSUserDomainMask , YES ) objectAtIndex : 0];
// file1
NSString *file1= @"file1.doc" ;
NSURL *url1 = [NSURL URLWithString : @"http://10.7.13.111:8080/WebContent/file/file1.doc"];
// 先创建文件 file1 ,再用 NSFileHandle 打开它
NSString *path1=[userDocPath stringByAppendingPathComponent :file1];
bool b=[fm createFileAtPath :path1 contents : nil attributes : nil];
NSFileHandle *fh1;
__block uint fSize1= 0 ; // 以 B 为单位,记录已下载的文件大小 , 需要声明为块可写
if (b){
fh1=[NSFileHandle fileHandleForWritingAtPath :path1];
}
// file2
NSString *file2= @"file2.png" ;
NSURL *url2 = [NSURL URLWithString : @"http://10.7.13.111:8080/WebContent/file/file2.png"];
// 先创建文件 file2 ,再用 NSFileHandle 打开它
NSString *path2=[userDocPath stringByAppendingPathComponent :file2];
b=[fm createFileAtPath :path2 contents : nil attributes : nil];
NSFileHandle *fh2;
__block uint fSize2= 0 ; // 以 B 为单位,记录已下载的文件大小 , 需要声明为块可写
if (b){
fh2=[NSFileHandle fileHandleForWritingAtPath :path2];
}
//////////////////////////// 任务队列 /////////////////////////////
if (! networkQueue ) {
networkQueue = [[ASINetworkQueue alloc] init];
}
failed = NO;
[networkQueue reset]; // 队列清零
[networkQueue setDownloadProgressDelegate : progress_total]; // 设置 queue 进度条
[networkQueue setShowAccurateProgress : YES]; // 进度精确显示
[networkQueue setDelegate : self]; // 设置队列的代理对象
ASIHTTPRequest *request;
///////////////// request for file1 //////////////////////
request = [ASIHTTPRequest requestWithURL :url1]; // 设置file1的 url
[request setDownloadProgressDelegate : progress_file1]; // file1的下载进度条
// 设置 userInfo ,可用于识别不同的 request 对象
[request setUserInfo :[NSDictionary dictionaryWithObject :file1 forKey : @"file1"]];
// 使用 complete 块,在下载完时做一些事情
[request setCompletionBlock :^( void ){
NSLog ( @"%@ complete !" ,file1);
assert (fh1);
// 关闭 file1
[fh1 closeFile];
}];
// 使用 failed 块,在下载失败时做一些事情
[request setFailedBlock :^( void ){
failed = YES;
NSLog ( @"%@ download failed !" ,file1);}
];
// 使用 received 块,在接受到数据时做一些事情
[request setDataReceivedBlock :^( NSData * data){
fSize1+=data. length ;
[status_file1 setText :[NSString stringWithFormat : @"%i B" ,fSize1]];
[status_total setText :[NSString stringWithFormat : @"%.0f %%" , progress_total . progress * 100]];
if (fh1!= nil ) {
[fh1 seekToEndOfFile];
[fh1 writeData :data];
}
NSLog ( @"%@:%u" ,file1,data.length );
}];
[networkQueue addOperation :request];
///////////// request for file2 //////////////////
request = [[ASIHTTPRequest alloc] initWithURL :url2]; // 设置file2的 url
[request setDownloadProgressDelegate : progress_file2]; // file2的下载进度条
[request setUserInfo :[NSDictionary dictionaryWithObject :file2 forKey : @"file2"]];
// 使用 complete 块,在下载完时做一些事情
[request setCompletionBlock :^( void ){
NSLog ( @"%@ complete !" ,file2);
assert (fh2);
// 关闭 file2
[fh2 closeFile];
}];
// 使用 failed 块,在下载失败时做一些事情
[request setFailedBlock :^( void ){
failed = YES;
NSLog ( @"%@ download failed !" ,file2);
}];
// 使用 received 块,在接受到数据时做一些事情
[request setDataReceivedBlock :^( NSData * data){
fSize2+=data. length ;
[status_file2 setText :[NSString stringWithFormat : @"%i B" ,fSize2]];
[status_total setText :[NSString stringWithFormat : @"%.0f %%" , progress_total . progress * 100]];
if (fh2!= nil ) {
[fh2 seekToEndOfFile];
[fh2 writeData :data];
}
}];
[networkQueue addOperation :request];
[networkQueue go]; // 队列任务开始
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货