iOS 檔案下載,ios檔案下載

來源:互聯網
上載者:User

iOS 檔案下載,ios檔案下載

iOS 視頻音樂類等應用會用到“檔案下載”。檔案下載在iOS中的實現如下:

1、小檔案下載
@interface ViewController () <NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UIProgressView *progressView;/** 檔案資料 */@property (nonatomic, strong) NSMutableData *fileData;/** 檔案的總長度 */@property (nonatomic, assign) NSInteger contentLength;@end

 

- (void)viewDidLoad {    [super viewDidLoad];    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"];    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];}#pragma mark - <NSURLConnectionDataDelegate>- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{    self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue];    self.fileData = [NSMutableData data];}- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{    [self.fileData appendData:data];    CGFloat progress = 1.0 * self.fileData.length / self.contentLength;    NSLog(@"已下載:%.2f%%", (progress) * 100);    self.progressView.progress = progress;}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    NSLog(@"下載完畢");        // 將檔案寫入沙箱中        // 快取檔案夾    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];        // 檔案路徑    NSString *file = [caches stringByAppendingPathComponent:@"minion_15.mp4"];        // 寫入資料    [self.fileData writeToFile:file atomically:YES];    self.fileData = nil;}

 

2、大檔案下載
#define XMGFile [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"minion_15.mp4"]@interface ViewController () <NSURLConnectionDataDelegate>@property (weak, nonatomic) IBOutlet UIProgressView *progressView;/** 檔案的總長度 */@property (nonatomic, assign) NSInteger contentLength;/** 當前下載的總長度 */@property (nonatomic, assign) NSInteger currentLength;/** 檔案控制代碼對象 */@property (nonatomic, strong) NSFileHandle *handle;@end

 

- (void)viewDidLoad {    [super viewDidLoad];        NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_15.mp4"];    [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:url] delegate:self];}

 

#pragma mark - <NSURLConnectionDataDelegate>/** * 接收到響應的時候:建立一個空的檔案 */- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{    // 獲得檔案的總長度    self.contentLength = [response.allHeaderFields[@"Content-Length"] integerValue];        // 建立一個空的檔案    [[NSFileManager defaultManager] createFileAtPath:XMGFile contents:nil attributes:nil];        // 建立檔案控制代碼    self.handle = [NSFileHandle fileHandleForWritingAtPath:XMGFile];}/** * 接收到具體資料:馬上把資料寫入一開始建立好的檔案 */- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{        // 指定資料的寫入位置 -- 檔案內容的最後面    [self.handle seekToEndOfFile];        // 寫入資料    [self.handle writeData:data];        // 拼接總長度    self.currentLength += data.length;        // 進度    self.progressView.progress = 1.0 * self.currentLength / self.contentLength;}- (void)connectionDidFinishLoading:(NSURLConnection *)connection{    // 關閉handle    [self.handle closeFile];    self.handle = nil;        // 清空長度    self.currentLength = 0;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.