简体   繁体   English

将文件写入NSDocument目录在iPhone中显示UIProgressView?

[英]Write file to NSDocument directory show UIProgressView in iPhone?

I have a pdf file from server. 我有来自服务器的pdf文件。

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. 然后,我需要将该文件加载到NSDocument目录路径中,并且工作正常,但是我想显示UIProgressView以存储每个字节。 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: 您可以使用NSURLConnection类来实现进度条:

.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. 让我知道任何疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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