檔案下載 NSURLConnection——小檔案下載

來源:互聯網
上載者:User

標籤:pre   component   ted   適合   nec   dfa   finish   writing   控制代碼   

1.下載小檔案,只適合幾百k,1、2M的檔案

//1.設定網址//2.載入請求//3.設定代理//4.實現代理方法下載檔案 NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492791082124&di=fc642407b4ec19430334653a9b873cff&imgtype=0&src=http%3A%2F%2Fi0.szhomeimg.com%2FUploadFiles%2FBBS%2F2006%2F10%2F24%2F27567833_1950.442.jpg"];   NSURLRequest *request = [NSURLRequest requestWithURL:url];[[NSURLConnection alloc] initWithRequest:request delegate:self];代理實現#pragma mark ---NSURLConnectionDelegate- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{    NSLog(@"下載失敗%@",error);}//下載響應- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{//擷取檔案大小    _totalSize = response.expectedContentLength;}//下載資料- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    //拼接資料    [_mudata appendData:data];    //擷取當前進度    NSLog(@"%f",1.0*_mudata.length/_totalSize);}//下載完成- (void)connectionDidFinishLoading:(NSURLConnection *)connection{        NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"1.jpg"];    [_mudata writeToFile:fullPath atomically:YES];}

  

  缺點:[_mudata appendData:data],會使記憶體暴漲,而且下載完畢後記憶體不會下降

2.通過控制代碼來下載大檔案

#pragma mark ----------------------#pragma mark NSURLConnectionDataDelegate-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    NSLog(@"didReceiveResponse");        //1.得到檔案的總大小(本次請求的檔案資料的總大小)    self.totalSize = response.expectedContentLength;        //2.寫資料到沙箱中    self.fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"123.mp4"];        //3.建立一個空的檔案    [[NSFileManager defaultManager] createFileAtPath:self.fullPath contents:nil attributes:nil];        //4.建立檔案控制代碼(指標)    self.handle = [NSFileHandle fileHandleForWritingAtPath:self.fullPath];}-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    //1.移動檔案控制代碼到資料的末尾    [self.handle seekToEndOfFile];        //2.寫資料    [self.handle writeData:data];        //3.獲得進度    self.currentSize += data.length;        //進度=已經下載/檔案的總大小    NSLog(@"%f",1.0 *  self.currentSize/self.totalSize);    self.progressView.progress = 1.0 *  self.currentSize/self.totalSize;    //NSLog(@"%@",self.fullPath);}-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    //1.關閉檔案控制代碼    [self.handle closeFile];    self.handle = nil;    NSLog(@"connectionDidFinishLoading");    NSLog(@"%@",self.fullPath);}

 3.通過斷點續傳來下載大檔案

佈建要求頭,如果檔案存在就繼續下載

同上    //2.建立請求對象    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    NSString *range = [NSString stringWithFormat:@"bytes=%zd-",self.currentSize];    [request setValue:range forHTTPHeaderField:@"Range"];    //同上}#pragma mark ----------------------#pragma mark NSURLConnectionDataDelegate-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{    //1.得到檔案的總大小(本次請求的檔案資料的總大小)    self.totalSize = response.expectedContentLength+self.currentSize;    if (self.currentSize > 0) {        return;    }    //同上-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    //同上}-(void)connectionDidFinishLoading:(NSURLConnection *)connection{    //同上}

 缺點:下次運行工程進來就要重新下載  

 

檔案下載 NSURLConnection——小檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.