美文网首页
轻量级进度下载 - OTHTTPRequest

轻量级进度下载 - OTHTTPRequest

作者: 居然是村长 | 来源:发表于2016-05-09 21:56 被阅读145次

https://github.com/OpenFibers/OTHTTPRequest
项目中用到了,使用简单,还不错,非常轻量级的下载库,支持下载进度与下载速度,预期大小与当前下载大小。

下载

1)首先在下载的时候是存储在temp文件夹下;
2)下载完成之后,会写入到Documents 下;

- (void)start {
    if (_request) {
        [_request pause];
        _request = nil;
    }

    // 下载网址
    NSString *downloadURLString = @"http://dl.google.com/drive/installgoogledrive.dmg";
    // 保存路径文件夹
    NSString *documentPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    _request = [[OTHTTPDownloadRequest alloc] initWithURL:downloadURLString
                                                cacheFile:[documentPath stringByAppendingPathComponent:@"temp"]
                                         finishedFilePath:[documentPath stringByAppendingPathComponent:[downloadURLString lastPathComponent]]];
    _request.delegate = self;
    [_request start];
}

- (void)pause {
    [_request pause];
}

// 下载信息等
- (void)downloadRequest:(OTHTTPDownloadRequest *)request
 currentProgressUpdated:(float)progress
                  speed:(float)bytesPerSecond
          totalReceived:(unsigned long long)totalReceived
       expectedDataSize:(unsigned long long)expectedDataSize {
    NSString *logInfo = [NSString stringWithFormat:
                         @"Download URL:\n%@\nprogress:%.2f %%\n\ndownloaded size:%.2fMB\n\nexpected size:%.2fMB\n\ncurrent speed:%.2f MB/s",
                         [request requestURL],
                         progress * 100,
                         [request downloadedFileSize] / (double) (1024 * 1024),
                         expectedDataSize / (double) (1024 * 1024),
                         request.averageDownloadSpeed / (double) (1024 * 1024)];
}

// 下载完成
- (void)downloadRequestFinished:(OTHTTPDownloadRequest *)request {
    NSString *logInfo = [NSString stringWithFormat:
                         @"Download URL Finished:\n%@\n\nexpected size:%.2fMB",
                         [request requestURL],
                         [request expectedFileSize] / (double) (1024 * 1024)];
}

// 下载失败
- (void)downloadRequestFailed:(OTHTTPDownloadRequest *)request error:(NSError *)error {
    NSString *logInfo = [NSString stringWithFormat:
                         @"Download URL Failed:\n%@\n\n%@",
                         [request requestURL],
                         error];
}

// 文件保存失败
-(void)downloadRequestWriteFileFailed:(OTHTTPDownloadRequest *)request exception:(NSException *)exception {
    NSString *logInfo = [NSString stringWithFormat:
                         @"Write file failed:\n%@",
                         [request cacheFilePath]];
}

上传

上传,可以取消。

- (void)upload {
    if (_request) {
        [_request cancel];
        _request = nil;
    }
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png"];
    
    NSString *uploadURLString = @"upload";
    _request = [[OTHTTPRequest alloc] initWithURL:[NSURL uploadURLString]];
    [_request addPostValue:@"post_value" forKey:@"key"];
    [_request addFileForKey:@"file" filePath:filePath fileName:@"Default.png" MIMEType:@"png"];

    _request.delegate = self;
    [_request start];
}

- (void)cancel {
    [_request cancel];
}

// 上传完成
- (void)otHTTPRequestFinished:(OTHTTPRequest *)request {
    NSString *responseString = request.responseString;
    _infoView.text = responseString;
}

// 上传失败
- (void)otHTTPRequestFailed:(OTHTTPRequest *)request error:(NSError *)error {
    NSString *errorString = [NSString stringWithFormat:@"Request failed with error: %@", error];
}

// 上传信息等
- (void)otHTTPRequest:(OTHTTPRequest *)request uploadProgressUpdated:(float)uploadProgress speed:(float)bytesPerSecond bytesSent:(unsigned long long)totalSent contentLength:(unsigned long long)contentLength {
    NSString *logInfo = [NSString stringWithFormat:
                         @"Upload URL:\n%@\nprogress:%.2f %%\n\nuploaded size:%.2fKB\n\nexpected size:%.2fKB\n\ncurrent speed:%.2f KB/s",
                         [request URL],
                         uploadProgress * 100,
                         totalSent / (double) (1024),
                         contentLength / (double) (1024),
                         request.averageUploadSpeed / (double) (1024)];
}

相关文章

网友评论

      本文标题:轻量级进度下载 - OTHTTPRequest

      本文链接:https://www.haomeiwen.com/subject/qaverttx.html