IOS file downloads and ios file downloads

Source: Internet
Author: User

IOS file downloads and ios file downloads

Apps such as iOS video and music will use "File Download ". The implementation of File Download in iOS is as follows:

1. Download small files
@ Interface ViewController () <NSURLConnectionDataDelegate> @ property (weak, nonatomic) IBOutlet UIProgressView * progressView;/** file data */@ property (nonatomic, strong) NSMutableData * fileData; /** total file length */@ 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-<signature>-(void) connection :( NSURLConnection *) connection response :( 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 (@ "downloaded: %. 2f % ", (progress) * 100); self. progressView. progress = progress;}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {NSLog (@ "download completed "); // write the file to the sandbox. // The cache folder NSString * caches = [Export (NSCachesDirectory, NSUserDomainMask, YES) lastObject]; // the file path NSString * file = [caches stringByAppendingPathComponent: @ "minion_15.mp4"]; // write data to [self. fileData writeToFile: file atomically: YES]; self. fileData = nil ;}

 

2. Download large files
# Define XMGFile [[callback (NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: @ "minion_15.mp4"] @ interface ViewController () <strong> @ property (weak, nonatomic) IBOutlet UIProgressView * progressView;/** total file length */@ property (nonatomic, assign) NSInteger contentLength;/** total downloaded length */@ property (nonatomic, assign) NSInteger currentLength;/** file handle object */@ 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>/*** when a response is received: Create an empty file */-(void) connection :( NSURLConnection *) connection didReceiveResponse :( NSHTTPURLResponse *) response {// get the total length of the file self. contentLength = [response. allHeaderFields [@ "Content-Length"] integerValue]; // create an empty file [[NSFileManager defaultManager] createFileAtPath: XMGFile contents: nil attributes: nil]; // create a file handle self. handle = [NSFileHandle fileHandleForWritingAtPath: XMGFile];}/*** receives specific data: Write the data to the file created at the beginning immediately */-(void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data {// specify the data write location -- the last part of the file content [self. handle seekToEndOfFile]; // write data [self. handle writeData: data]; // total stitching length self. currentLength + = data. length; // progress self. progressView. progress = 1.0 * self. currentLength/self. contentLength;}-(void) connectionDidFinishLoading :( NSURLConnection *) connection {// close handle [self. handle closeFile]; self. handle = nil; // clear the length self. currentLength = 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.