简体   繁体   中英

Write file to NSDocument directory show UIProgressView in iPhone?

I have a pdf file from server.

Then I need to load the file to NSDocument directory path and its working fine, but i want to show UIProgressView for store each bytes. How to do this , please help me

Thanks in Advance

I tried to store like this:

            NSError *error;
            NSArray *ipaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *idocumentsDir = [ipaths objectAtIndex:0];
            NSString *idataPath = [idocumentsDir stringByAppendingPathComponent:@"File"];
            NSLog(@"idataPath:%@",idataPath);

            //Create folder here
            if (![[NSFileManager defaultManager] fileExistsAtPath:idataPath])
            {
                [[NSFileManager defaultManager] createDirectoryAtPath:idataPath withIntermediateDirectories:NO attributes:nil error:&error];
            }
  // Image Download here
            NSString *fileName = [idataPath stringByAppendingFormat:@"/image.jpg"];
            NSLog(@"imagePathDOWNLOAD:%@",fileName);

            NSData *pdfData1 = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[URLArray objectAtIndex:a]]];
            [pdfData1 writeToFile:fileName atomically:YES];

You can use NSURLConnection class to implement progress bar:

.h:
NSMutableData *responseData;
NSString *originalFileSize;
NSString *downloadedFileSize;

.m:
- (void)load {
    NSURL *myURL = [NSURL URLWithString:@""];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL
                                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                         timeoutInterval:60];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
originalFileSize = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];

downloadedFileSize = [responseData length];
progressBar.progress = ([originalFileSize floatValue] / [downloadedFileSize floatValue]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[responseData release];
[connection release];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
NSLog(@"Succeeded! Received %d bytes of data",[responseData lengtn]);

}

Let me know for any queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM