关于ASIHttpRequest文件下载时获取到的文件长度不一致问题

aking21alinjuju 2012-10-29 08:03:24
最近尝试着使用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]; // 队列任务开始
}
...全文
256 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
lemontree0317 2012-11-01
  • 打赏
  • 举报
回复
下载plist文件呢。。会不会是原来的大小

29,028

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧