Nsurlconnection Large File Download

Source: Internet
Author: User

#import "ViewController.h"

@interface Viewcontroller () <nsurlconnectiondatadelegate>

@property (weak, nonatomic) iboutlet uiprogressview *progressview;

@property (nonatomic, assign) nsinteger totalsize;

@property (nonatomic, assign) nsinteger currentsize;

/** Sandbox Path * /

@property (nonatomic, strong) nsstring *fullpath;

/** Connection Object * /

@property (nonatomic, strong) nsurlconnection *connect;

/** Output Stream * /

@property (nonatomic, strong) nsoutputstream *stream;

@end

@implementation Viewcontroller

-(ibaction) Startbtnclick: (ID) Sender {

[self download];

}

-(ibaction) Cancelbtnclick: (ID) Sender {

[self. Connect cancel];

}

-(ibaction) Goonbtnclick: (ID) Sender {

[self download];

}

Memory Spike

-(void) Download

{

//1.url

nsurl *url = [nsurl urlwithstring:@ "http://www.33lc.com/article/UploadPic/2012-10/ 2012102514201759594.jpg "];

//2. Create Request Object

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl: url];

// Set the request header information to tell the server value to request part of the data range

/*

bytes=0-100

bytes=-100

bytes=0- all data after the request

*/

nsstring *range = [nsstring stringwithformat:@ "bytes=%zd-",self. CurrentSize];

[Request SetValue: Range Forhttpheaderfield:@ "range"];

NSLog(@ "+++++++%@", Range);

//3. Send Request

nsurlconnection *connect = [[nsurlconnection alloc]initwithrequest: Request delegate:self];

self. Connect = connect;

}

#pragma Mark----------------------

#pragma Mark Nsurlconnectiondatadelegate

-(void) connection: (nsurlconnection *) connection didreceiveresponse: (nsurlresponse * ) Response

{

NSLog(@ "Didreceiveresponse");

//1. Gets the total size of the file (the total size of the file data for this request!! = file )

//Self.totalsize = Response.expectedcontentlength + self.currentsize;

if (self. CurrentSize >0) {

return;

}

self. TotalSize = response. Expectedcontentlength;

//2. write the data into the sandbox

self. FullPath = [[nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask , YES) lastobject]stringbyappendingpathcomponent:@ "123.jpg"];

NSLog(@ "%@",self. FullPath);

//3. Create an output stream

Nsoutputstream

Nsinputstream

/*

first parameter : the path to a file

second parameter : YES append

feature : if the output stream points to an address that does not have a file, an empty file is created automatically

*/

nsoutputstream *stream = [[nsoutputstream alloc]inittofileatpath:Self . fullPath append:YES];

// open output stream

[Stream open];

self. Stream = stream;

}

-(void) connection: (nsurlconnection *) connection didreceivedata: (nsdata *) Data

{

// Write Data

[self. Stream write:d ata. Bytes maxLength:d ata. Length];

//3. Get Progress

self. CurrentSize + = data. Length;

// Progress = Total size of downloaded/file

NSLog(@ "%f",1.0 * self. CurrentSize/self. TotalSize);

self. Progressview. Progress = 1.0 * self. CurrentSize/self. TotalSize;

//nslog (@ "%@", Self.fullpath);

}

-(void) connection: (nsurlconnection *) connection didfailwitherror: (nserror *) Error

{

}

-(void) connectiondidfinishloading: (nsurlconnection *) connection

{

// close stream

[self. Stream close];

self. Stream = nil;

NSLog(@ "connectiondidfinishloading");

NSLog(@ "%@",self. FullPath);

}

@end

Nsurlconnection Large File Download

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.