大文件的下载(显示进度条)

//
//  ViewController.m
//  大文件的下载

/*
 如果文件比较小,下载方式会比较多
 直接用NSData的+ (id)dataWithContentsOfURL:(NSURL *)url;
 利用NSURLConnection发送一个HTTP请求去下载
 如果是下载图片,还可以利用SDWebImage框架
 */

#import "ViewController.h"

@interface ViewController ()<NSURLConnectionDataDelegate>

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;
- (IBAction)start;

//写数据的文件句柄
@property(nonatomic,strong)NSFileHandle *writeHandle;
//当前已下载的数据的长度
@property(nonatomic,assign)long long currentLength;
//完整文件的总长度
@property(nonatomic,assign)long long totalLength;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)start
{
    NSURL *url = [NSURL URLWithString:@"https://localhost/resources/video.zip"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
}

#pragma mark - NSURLConnectionDataDelegate
/**
 *  请求失败的时候调用(请求超时、断网、没有网。一般都是客户端错误)
 */
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
   
}

/**
 *  当接受到服务器的响应的就会调用
 */
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //0.文件存储的路径
    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filepath = [caches stringByAppendingPathComponent:@"video.zip"];
    
    //1.创建一个空的文件到沙盒中
    NSFileManager *mgr = [NSFileManager defaultManager];
    [mgr createFileAtPath:filepath contents:nil attributes:nil];
    
    //2.创建写数据的文件句柄
    self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
    
    //3.获取完整文件的长度
    self.totalLength = response.expectedContentLength;
}

/**
 *  当接受到服务器返回的数据就会调用(可能会被调用多次,每次只会传递部分数据)
 */
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //累计已下载的数据的长度
    self.currentLength += data.length;
    //显示进度
    double progress = (double)self.currentLength / self.totalLength;
    self.progressView.progress = progress;
    
    //移到到文件的尾部
    [self.writeHandle seekToEndOfFile];
    //从当前移动的位置开始写入数据
    [self.writeHandle writeData:data];
}

/**
 *  当服务器的数据接受完毕后就会调用
 */
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //清空属性值
    self.currentLength = 0;
    self.totalLength = 0;
    
    //关闭连接(不再输入数据到文件中)
    [self.writeHandle closeFile];
    self.writeHandle = nil;
}

@end

在 jQuery 中实现大文件上传并显示进度条可以使用 XMLHttpRequest 对象和 FormData 对象来实现。以下是一个简单的示例代码: HTML: ```html <input type="file" id="fileInput"> <button id="uploadButton">上传</button> <div id="progressBar"></div> ``` JavaScript: ```javascript $(document).ready(function() { $('#uploadButton').click(function() { var fileInput = document.getElementById('fileInput'); var file = fileInput.files[0]; var formData = new FormData(); formData.append('file', file); var xhr = new XMLHttpRequest(); xhr.open('POST', 'upload.php', true); xhr.upload.onprogress = function(e) { if (e.lengthComputable) { var percent = Math.round((e.loaded / e.total) * 100); $('#progressBar').css('width', percent + '%').text(percent + '%'); } }; xhr.onload = function() { if (xhr.status === 200) { // 上传成功 $('#progressBar').text('上传完成'); } else { // 上传失败 $('#progressBar').text('上传失败'); } }; xhr.send(formData); }); }); ``` 在上面的代码中,我们首先获取文件输入框的文件,然后创建一个 FormData 对象,并将文件添加到其中。接下来,我们创建一个 XMLHttpRequest 对象,并设置请求的方法、URL 和异步标志。然后,我们通过监听 `xhr.upload.onprogress` 事件来获取上传进度,并更新进度条的宽度和文本。最后,我们监听 `xhr.onload` 事件,在上传完成后进行相应的处理。 请注意,上述代码中的 `upload.php` 是一个示例的服务器端处理文件上传的脚本,你需要根据自己的实际情况进行相应的修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值