Documents:Only documents and other data that is user-generated, or that cannot otherwise be recreated by your application, should be stored in the/Documents directory and will be automatically backed up by iCloud. //用户生成的数据,程序不可重新生成的数据,会通过iCloud备份
Library:Data that can be downloaded again or regenerated should be stored in the/Library/Caches directory. Examples of files you should put in the Caches directory include database cache files and downloadable content, such as that used by magazine, newspaper, and map applications. // 可被下载的数据
tmp: Data that is used only temporarily should be stored in the/tmp directory. Although these files are not backed up to iCloud, remember to delete those files when you are done with them so that they do not continue to consume space on the user's device. // 临时数据
2.切片
切片主要用到NSFileHandle这个类,其实就是通过移动文件指针来读取某段内容。
// model.filePath 文件路径
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:model.filePath];
// 移动文件指针
// kSuperUploadBlockSize 上传切片大小 这里是1M, i指已上传片数(i = model.uploadedCount)
[handle seekToFileOffset:kSuperUploadBlockSize * i];
//读取数据
NSData *blockData = [handle readDataOfLength:kSuperUploadBlockSize];
这里我将大文件切成最小1M的小文件来上传。这边使用到一个Model,该数据模型主要存放上传列表中所需要的一些基本数据。因为我们每次上传完一片,需要更新UI。由于这边需要支持断点续传,因此需要记录文件的进度值,已上传的片数我们需要保存下来。保存上传文件路径和文件进度可以使用数据库或则plist文件等方式,这边需要保存的数据不是很多,所以我直接保存在了偏好设置中。每片文件上传成功,设置该模型已上传片数,并且更新本地文件进度值。
我们可以大致看下所用到的Model
YJTUploadManager.h
#import <Foundation/Foundation.h>
@interface YJTDocUploadModel : NSObject
// 方便操作(暂停取消)正在上传的文件
@property (nonatomic, strong) NSURLSessionDataTask *dataTask;
// 总大小
@property (nonatomic, assign) int64_t totalSize;
// 总片数
@property (nonatomic, assign) NSInteger totalCount;
// 已上传片数
@property (nonatomic, assign) NSInteger uploadedCount;
// 上传所需参数
@property (nonatomic, copy) NSString *upToken;
// 上传状态标识, 记录是上传中还是暂停
@property (nonatomic, assign) BOOL isRunning;
// 缓存文件路径
@property (nonatomic, copy) NSString *filePath;
// 用来保存文件名使用
@property (nonatomic, copy) NSString *lastPathComponent;
// 以下属性用于给上传列表界面赋值
@property (nonatomic, assign) NSInteger docType;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *progressLableText;
@property (nonatomic, assign) CGFloat uploadPercent;
@property (nonatomic, copy) void(^progressBlock)(CGFloat uploadPersent,NSString *progressLableText);
// 保存上传成功后调用保存接口的参数
@property (nonatomic, strong) NSMutableDictionary *parameters;
@end
YJTUploadManager.m
#import "YJTDocUploadModel.h"
@implementation YJTDocUploadModel
// 上传完毕后更新模型相关数据
- (void)setUploadedCount:(NSInteger)uploadedCount {
_uploadedCount = uploadedCount;
self.uploadPercent = (CGFloat)uploadedCount / self.totalCount;
self.progressLableText = [NSString stringWithFormat:@"%.2fMB/%.2fMB",self.totalSize * self.uploadPercent /1024.0/1024.0,self.totalSize/1024.0/1024.0];
if (self.progressBlock) {
self.progressBlock(self.uploadPercent,self.progressLableText);
}
// 刷新本地缓存
[[YJTUploadManager shareUploadManager] refreshCaches];
}
@end










